Static Import
Static import feature was introduced in Java 5. The functionality provided by static import is it allows a class to access static members of another external class directly without qualifying it with the class name as if it is declared within the called class. Static import reduces code size.
Program to demonstrate Static import:
//Java program to demonstrate static import
import static java.lang.System.*; //Static import for System class
class StaticImportExample
{
public static void main(String args[])
{
out.println("Hello");//Now no need of System.out
out.println("Demo");
}
}
Output:
Hello
Demo
Explanation of above program:
In above program static members of System class is imported in StaticImportExample class and thus it is able to use it's static member out without qualifying it with the class name System.
0 Comment(s)