In J2SE 5.0, Java language introduces a lot of new features, this article will mainly introduce Static Import. Static Import's main solution is to facilitate developers to create and use global constants and static methods. To use this new feature, you should first download the latest J2SDK 5.0 from java.sun.com.
In the "Effective Java", it has been talked about defining constants in the interface. We should always use interfaces to define types. But in the actual development work, there are still many people to use interfaces, the reason why they do this is that this definition constant is very convenient. For example, as follows: public interface badirrationalconstants {public static final double sqrt_two = 1.414; public static final double sqrt_three = 1.732;}
Public Interface BadtranscendentalconStants {public static final double pi = 3.14159; public static final double e = 2.71828;} If you want to use the constants defined in these interfaces in your class, then you have to implement these interfaces. For example, Public Class BaduseOfconStants Implements BadtranscendentalconStants, BadirrationalConstants {
Public Static Double SinpiOverfour () {Return SQRT_TWO / 2;}
Public static void main (string [] args) {system.out.println ("pi is approximately" pi); system.out.println ("The sin of pi / 4 is about" sinpioverfour ());}} This constant is part of your class. If this class is not final, the other classes have inherited this class again, so that it inherits these constants, but this may not be the result of the user.
In response to this situation, we can do this, which is to define these constants in a class, you can access them via classname.variablename when using. For example, Package StaticEx;
public class IrrationalConstants {public static final double SQRT_TWO = 1.414; public static final double SQRT_THREE = 1.732;} package staticEx; public class TranscendentalConstants {public static final double PI = 3.14159; public static final double E = 2.71828;} now J2SE 5.0 provides Static imported features, you only need to write a static keyword after the import keyword, you can use the constants defined in the class, such as import static static static static static static static static static static static sitex.irrationalconstants.sqrt_three; import static staticex. TranscendentalConstants.PI;. * of course you can also use the format, such as import static staticEx.IrrationalConstants *;. the format package staticEx; import static staticEx.IrrationalConstants.SQRT_TWO; import static staticEx.IrrationalConstants.SQRT_THREE; import static staticEx.TranscendentalConstants. PI; public class ConstantsWithStaticImport {public static double sinPiOverFour () {return SQRT_TWO / 2;} public static void main (String [] args) {System.out.println ( "Pi is approximately" PI); Syste M.out.println ("THE SIN OF PI / 4 IS ABOUT" SINPIOVERFOUR ());}} Running the program will get PI is approximately 3.14159 The sin of pi / 4 is about 0.707 here remind everyone if you Excessive use. * Style, then it may have a negative impact on the readability of the program. Because it is difficult to see where this variable is defined.
This is also the same for constant definitions, and the use of static methods is also applicable. For example, the following example package staticEx; public class IrrationalConstants2 {public static final double SQRT_TWO = 1.414; public static final double SQRT_THREE = 1.732; public static double sinPiOverFour () {return SQRT_TWO / 2;}} package staticEx; import static staticEx.IrrationalConstants2. *; import static staticEx.TranscendentalConstants *;. public class ConstantsWithStaticImport2 {public static void main (String [] args) {System.out.println ( "Pi is approximately" PI); System.out.println ( "The sin of PI / 4 IS ABOUT " SINPIOVERFOUR ());}} After static imports IrrationalConstants2 in the ConstantSwithStaticImport2 class, you can use its static method SINPIOVERFOUR (). The above brief introduction how to use Static Import, you can refer to the connection to get more introduction: http://java.sun.com/developer/jdctechtips/2004/tt1005.htmlhttp: //java.sun.com/j2se / 1.5.0/docs/guide/language/static-import.html