Static Import in Java with an Example
What is Static Import:
Static Import is a feature added in Java 1.5(released in Sep 30 2004 almost 10 years back!) along with Var-args,Auto-Boxing and Auto-Unboxing, Generics, Enhanced for-loop,Enums,Scanner Class and Annotations. Using this Static Import feature we can import static members of a class and as well we can access all non-private static members without using class name from other classes.
Syntax:
To understand static imports, let us see the differences between statements.
Some of the Valid Syntax of static imports
Reference Books:
Static Import is a feature added in Java 1.5(released in Sep 30 2004 almost 10 years back!) along with Var-args,Auto-Boxing and Auto-Unboxing, Generics, Enhanced for-loop,Enums,Scanner Class and Annotations. Using this Static Import feature we can import static members of a class and as well we can access all non-private static members without using class name from other classes.
Syntax:
import static packagename.classname.*; or import static packagename.classname.static member name;Above syntax allows to all static members of the class , Second syntax allows only to call specified static member to import.
To understand static imports, let us see the differences between statements.
import P1.*; ----> have access to all classes of package import P1.A.*; ----> have access to only A import static P1.A.*; ----> have access to all static members of A class import static P1.A.a; ----> have access to only the static variable "a". import static P1.A.m1; ----> have access to only the static method "m1";Example program to use System and Math class static members
package com.javabynataraj.core; import static java.lang.System.out; import static java.lang.Math.PI; import java.sql.Timestamp; //http://javabynataraj.blogspot.com public class StaticImport { public static void main(String[] args) { out.println("Static Imports Test"); out.println("-------------------"); out.println("PI Value: "+PI); //directly using PI value long time = System.currentTimeMillis(); java.sql.Timestamp ts=new Timestamp(time); out.println("Current Time and Date: "+ts); } }Output:
Static Imports Test ------------------- PI Value: 3.141592653589793 Current Time and Date: 2014-12-25 18:42:48.161In the above program we are not using any package.ClassA obj = new package.ClassA(); like normal way, Here we can be used without any package references. Static import declaration imports static members from classes and allowing them to be used without class reference. Local static members or Imported static members execute first? Answer: Local class members are called first, because local members has first priority. Example program:
package com.javabynataraj.core; import static java.lang.System.*; //http://javabynataraj.blogspot.com public class TestStaticImports { static int out=50; public static void main(String[] args) { System.out.println("System out: "+System.out); System.out.println("Local out: "+out); out.println(); //Error - Cannot invoke println() on the primitive type int } }Above program we have declared out as a local variable, so we can not call out.println using static impoirt.
Some of the Valid Syntax of static imports
import java.lang.*; //Correct import java.lang.System; //Correct import java.lang.System.*; //Correct import static java.lang.System.*; //Correct import static java.lang.System.out; //Correct import static java.lang.System; //inCorrect import static java.lang.System.out.*; //inCorrect static import java.lang.System.out; //inCorrectNOTE: static import statements are written as "import static" in code but not "static import".
Reference Books: