Static vs Normal vs No imports in JAVA

Why imports are useful:

An import declaration allows a static member or a named type to be referred to by a simple name that consists of a single identifier. Without the use of an appropriate import declaration, the only way to refer to a type declared in another package, or a static member of another type, is to use a fully qualified name.

A single-type-import declaration imports a single named type, by mentioning its canonical name.

A type-import-on-demand declaration imports all the accessible types of a named type or package as needed. It is a compile time error to import a type from the unnamed package.

A single static import declaration imports all accessible static members with a given name from a type, by giving its canonical name.

A static-import-on-demand declaration imports all accessible static members of a named type as needed.

http://stackoverflow.com/questions/2903166/meaning-of-the-import-statement-in-a-java-file

What happen if we don’t declare imports in JAVA file:

The below program I am trying to call one of the method add of Arraylist i.e java.util.Arraylist i.e https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#add(E) .As i didn’t declared any imports in my .java file it throws error like below.

public class StaticVsNormalVsNo_Imports{
public static void main(String[] args)throws Exception{
Arraylist arraylist=new Arraylist();
System.out.println(arraylist.add());
}
}

o/p:
D:\OCJP\imports>javac StaticVsNormalVsNo_Imports.java
StaticVsNormalVsNo_Imports.java:4: error: cannot find symbol
System.out.println(arraylist.add());
^
symbol: method add()
location: variable arraylist of type Arraylist
1 error

If we try to  get rid of the error by doing some changes like creating my own Arraylist class as below.

public class StaticVsNormalVsNo_Imports{
public static void main(String[] args)throws Exception{
Arraylist arraylist=new Arraylist();
System.out.println(arraylist.add(10));
System.out.println(arraylist);
}
}

class Arraylist{
public String add(int a){
return “Mehtod message……..”+a;
}
}

o/p:
D:\OCJP\imports>javac StaticVsNormalVsNo_Imports.java

D:\OCJP\imports>java StaticVsNormalVsNo_Imports
Mehtod message……..10
Arraylist@15db9742

But it is not the functionality what i intended to do  so I move one step forward and I am giving here the fully qualified name i.e java.util.Arraylist<E>.

public class StaticVsNormalVsNo_Imports{
public static void main(String[] args)throws Exception{
java.util.ArrayList<Integer> arraylist=new java.util.ArrayList<Integer>();
System.out.println(arraylist.add(10));
System.out.println(arraylist);
}
}

class Arraylist{
public String add(int a){
return “Mehtod message……..”+a;
}
}

o/p:
D:\OCJP\imports>java StaticVsNormalVsNo_Imports
true
[10]

Now functionality is working as expected ,but what if i want’s to use 100 or 10 arraylists.In this case writing of java.util.ArrayList<Integer> arraylist=new java.util.ArrayList<Integer>(); 100 times or 10 times doesn’t make sense.So here java people import statement to rescue.

import java.util.ArrayList;

public class StaticVsNormalVsNo_Imports{
public static void main(String[] args)throws Exception{
ArrayList<Integer> arraylist=new .ArrayList<Integer>();
System.out.println(arraylist.add(10));
System.out.println(arraylist);
}
}

o/p:

true [10]

What import java.util.ArrayList saying is whenever compiler sees the word ArrayList in the class it will use the java.util.ArrayList i.e in the JAVA API document there is a package called util and in that package there is class called ArrayList.

Here A package may contains many number of classes if i want to use one or more classes in a perticular package ,We are having an option wildcard character(*).If compiler sees the * symobol in import declaration it will search for all the classes in the entire package.

import java.util.*;

public class StaticVsNormalVsNo_Imports{
public static void main(String[] args)throws Exception{
ArrayList<Integer> arraylist=new ArrayList<Integer>();
System.out.println(arraylist.add(10));
System.out.println(arraylist);

//Key shouldn’t be null in treemap
//If we put two keys similar it will ovverride
//multiple Null values are allowed
TreeMap<String,String> treeMap=new TreeMap<String,String>();
treeMap.put(“null”,null);
treeMap.put(“null1”,null);
treeMap.put(“null”,”nuller”);
treeMap.put(“null2″,null);
System.out.println(treeMap);

//one key can be null
//If we put two keys similar it will ovverride
HashMap<String,String> hashMap=new HashMap<String,String>();
hashMap.put(null,null);
hashMap.put(null,”null1”);
hashMap.put(“null1”,null);
hashMap.put(“null2″,null);
hashMap.put(null,”nuller”);
System.out.println(hashMap);

//No nulls as keys as well as values too
Hashtable<String,String> hashTable=new Hashtable<String,String>();
hashTable.put(“null”,”null-1″);
hashTable.put(“null1″,”null”);
hashTable.put(“null1″,”nuller”);
System.out.println(hashTable);

}
}

o/p:

D:\OCJP\imports>java StaticVsNormalVsNo_Imports
true
[10]
{null=nuller, null1=null, null2=null}
{null=nuller, null1=null, null2=null}
{null=null-1, null1=nuller}

static imports in JAVA:

If we want to access the static members and methods of a class with out using the classname evrytime i.e Classname.XXXXX etc.Here is the example

System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.toHexString(123));

instead of putting class name every time we will go for static import.

import java.util.*;
import java.util.concurrent.*;

import static java.lang.System.out;
import static java.lang.Integer.*;
//static import won’t work
public class StaticVsNormalVsNo_Imports{
public static void main(String[] args)throws Exception{
out.println(MAX_VALUE);
out.println(MIN_VALUE);
out.println(toHexString(123));

o/p:

D:\OCJP\imports>javac StaticVsNormalVsNo_Imports.java

D:\OCJP\imports>java StaticVsNormalVsNo_Imports
2147483647
-2147483648
7b

but it is having dis advantage like

  • readability
  • both Long and Integer having MAX_VALUE etc similar properties in this case if we put wild card import(*)  it leads us to Compile Time Error.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s