Monday, 1 October 2012

Java Static Import

This is one of feature in Java 5.

In order to access static members, it is necessary to qualify references with the class they came from. For example, one must say:

double r = Math.cos(Math.PI * theta);
System.out.println(“Hi Welcome”);

Using Static import, we can simply call the method without qualifying the class that belongs to as follows:

import static java.lang.System.out;
import static
java.lang.Math.PI;
import static java.lang.Math.cos;

double r = cos(PI * theta);
out.println("Blah blah blah");

We can use this when we require to frequently access the static methods from one or more classes.  You can see the java documentation about static import here.

No comments:

Post a Comment