Wednesday, 3 December 2014

JAVA STATIC IMPORT - JAVA5 Feature

JAVA STATIC IMPORT

static import is used to access static members of a class without which you have to access static members using the class name.


Example:(without static import)


public class A {

public static void main(String[] args){

System.out.println("one");
System.out.println("two");
System.out.println("three");
System.out.println("four");
System.out.println("five");
System.out.println("six");


}


Example:(with static import)

import static java.lang.System.out; 

public class A {

public static void main(String[] args){

out.println("one");

out.println("two");

out.println("three");

out.println("four");

out.println("five");

out.println("six");

}
}



Output:

one
two
three
four
five
six

Here, output remains same but, we are accessing and printing Strings with preceding class name for static variable out


ADV: Reduces amount of code

No comments:

Post a Comment