MY mENU


Thursday, 9 February 2012

Static Import Statement


In some situations where you need frequent access to static final fields (constants) and static methods from one or two classes. Prefixing the name of these classes over and over can result in cluttered code.

The static import statement gives you a way to import the static constants and static methods that you want to use so that you do not need to prefix the name of their class.

The java.lang.Math class defines the PI constant and many static methods, as bellow

public static final double PI 3.141592653589793
public static double cos(double a)

You can use the static import statement to import the static members of java.lang.Math so that you don't need to prefix the class name, Math. Once they have been imported, thestatic members can be used without qualification.

Example
 :

import static java.lang.Math.*;
Class Trigonometry {
double r = cos(PI * theta);
------
}

No comments:

Post a Comment