top of page
Writer's pictureparthpatel051991

Import Statement in JAVA

Import statement

==============




=> We can resolve this problem by using fully qualified name "java.util.ArrayList"

l=new java.util.ArrayList();". But problem with using fully qualified name

every time is it increases length of the code and

reduces readability.

=> We can resolve this problem by using import statements.








Types of Import Statements:

There are 2 types of import statements.

1) Explicit class import

2) Implicit class import.


Explicit class import:

Example: Import java.util.ArrayList ;

=> This type of import is highly recommended to use because it improves

readability of the code.

=> Best suitable for developers where readability is important.


Implicit class import:

Example: import java.util.*;

=> It is never recommended to use because it reduces readability of the code.

=> Best suitable for students where typing is important.


=========================================================


"Import statement is totally compile time concept" if more no of imports are there

then more will be the compile time

but there is "no change in execution time".

Difference between C language #include and java language import ?

=======

1. It can be used in C & C++

2. At compile time only compiler copy the code from standard library and placed in

current program.

3. It is static inclusion

4. wastage of memory

Ex : <jsp:@ file="">


import

======

1. It can be used in Java

2. At runtime JVM will execute the corresponding standard library and use it's

result in current program.

3. It is dynamic inclusion

4. No wastage of memory

Ex : <jsp:include >



Note:

In the case of C language #include all the header files will be loaded at the time

of include statement hence it follows static loading.

But in java import statement no ".class" will be loaded at the time of import

statements in the next lines of the code whenever we are

using a particular class then only corresponding ".class" file will be loaded.

Hence it follows "dynamic loading" or "load-on -demand" or "load-on-fly".



JDK 1.5 versions new features :

  1. For-Each

  2. Var-arg

  3. Queue

  4. Generics

  5. Auto boxing and Auto unboxing

  6. Co-varient return types

  7. Annotations

  8. Enum

  9. Static import

  10. String builder



Static import:

This concept introduced in 1.5 versions. According to sun static import improves

readability of the code but according to

worldwide Programming exports (like us) static imports creates confusion and

reduces readability of the code. Hence if there is no

specific requirement never recommended to use a static import.

Usually we can access static members by using class name but whenever we are using

static import it is not require to use class name

we can access directly.







Note:

Two packages contain a class or interface with the same is very rare hence

ambiguity problem is very rare in normal import.

But 2 classes or interfaces can contain a method or variable with the same name is

very common hence ambiguity

problem is also very common in static import.

While resolving static members compiler will give the precedence in the following

order.

1. Current class static member

2. Explicit static import

3. implicit static import



Usage of static import reduces readability and creates confusion hence if there is no specific requirement never recommended to use static import.



What is the difference between general import and static import ?

normal import

============

  1. We can use normal imports to import classes and interfaces of a package.

  2. Whenever we are using normal import we can access class and interfaces directly by their short name it is not require to use fully qualified names.


static import

===========

  1. We can use static import to import static members of a particular class.

  2. Whenever we are using static import it is not require to use class name we can access static members directly.










18 views0 comments

Recent Posts

See All

Battle of the Backends: Java vs Node.js

Comparing Java and Node.js involves contrasting two distinct platforms commonly used in backend development. Here’s a breakdown of their...

コメント


bottom of page