Static Import Statement
1.5v new features
- for-each loop
- var-args
- AutoBoxing and AutoUnBoxing
- Generic Method
- co-variant return type.
- Queue
- Annotation
- enum
- static import
Static Import
- introduce in 1.5v
- according to sun uses of static import reduces the length of the code and improves readability but according world wide programming expert (like us uses of static import creates confusion and reduces readability hence if there is no specific requirement then it is not recommended to use static import.
- usually, we can access static members using the class names but whenever writing static import we can access static members directly without a class name.
without static import-:
class Test{
public static void main(String[] args){
System.out.print(Math.sqrt(4));
System.out.print(Math.max(10,20));
Sysstem.out.print(Math.random());
}
}
with Static import-:
import static java.lang.Math.*;
class Test{
public static void main(String[] args){
System.out.print(sqrt(4));
System.out.print(max(10,20));
System.out.print(random());
}
}
class Test{
static PrintStream out;
}
System.out.println();
explain about System.out.println()-:
System-: System is the class.
out-: out is the static variable present in the System class of the type PrintStream.
println()-:println() is the method present in the printStream.
out is a static variable present in the system class hence we can access by using the class name System but whenever we are writing static import it is not required a class name and we can access it directly.
import static java.lang.System.out;
class Test{
public static void main(String[] args){
out.println("hello");
out.println("hi");
}
}
class Test{
static String s="java";
}
Test.s.length();
where
Test-:Test is the class.
s-:s is the static variable present in the Test class of the type of java.lang.String.
length()-: length() is the method present in the String class.
import java.lang.Integer.*;
import java.lang.Byte.*;
class Test{
public static void main(String[] args){
System.out.print(MAX_VALUE);
}
}
compile time error-:reference to MAX_VALUE ambiguity problem
while resolving static member compiler will always consider the precedence in the following order
- current class static members
- Explicit static import
- Implicit static import
import static java.lang.Integer.MAX_VALUE;
import static java.lang.Byte.*;
class Test{
public static void main(String[] args){
static int MAX_VALUE=999;
System.out.print(MAX_VALUE);
}
}
output-: 999
import static java.lang.Integer.MAX_VALUE;
import static java.lang.Byte.*;
class Test{
public static void main(String[] args){
//static int MAX_VALUE=999;
System.out.print(MAX_VALUE);
}
}
if comment static member then static explicit static import and hence Integer class max_value will be considered in this case the output is .
output-:2147483647
//import static java.lang.Integer.MAX_VALUE;
import static java.lang.Byte.*;
class Test{
public static void main(String[] args){
//static int MAX_VALUE=999;
System.out.print(MAX_VALUE);
}
}
if we comment both static member and Explicit import then implicite static import will be consider in this case output is.
output-:127
Normal Import Syntax
Explicit import-:
import packageName.class;
eg-:
import java.util.ArrayList;
Implicit import-:
import packageName.*;
eg-:
import java.util.*;
Static Import Syntax
Explicit import-:
import static packageName.className.staticMethod/staticVariable;
eg-:
import static java.lang.Math.sqrt;
import static java.lang.System.out;
Implicit import-:
import static packageName.className.*;
eg-:
import static java.lang.Math.*;
import static java.lang.System.*;
Ques-: which of the following statements are valid?
import java.lang.Math.*; //invalid
import static java.lang.Math.*; //valid
import java.lang.Math.sqrt; //invalid
import static java.lang.Math.sqrt();//invalid
import java.lang.Math.sqrt.*; //invalid
import static java.lang.Math.sqrt;//valid
import java.lang; //invalid
import static java.lang; //invalid
import java.lang.*; //valid
import static java.lang.*; //invalid
- Two packages contain classes or interface with the same name is very rare and hence ambiguity problems very rare in normal import.
- but two classes and interface contain a variable are method with same name is very common and hence ambiguity problem is also very common problem in static import.
- uses of static import reduces readbility and creates confusion and hence if there is no specific requirement the its not recommended to use static import.
difference b/w normal import and static import -:
Normal Import-:
- we can use normal import to import classes and interface of a particular package
- whenever we are using normal import it is not required to use fully qualified name we can use short name directly
Static Import-:
- we can use static import to import static members of a particular class or interface
- whenever we are writing static import it is not required to use a class name to access static member and we can access them directly.