Static import of j2se 5.0 New Features

Source: Internet
Author: User
In j2se 5.0, Java introduces many new features. This article mainly introduces static import. Static import is mainly used to help developers create and use global constants and static methods. To use this new feature, you must first download the latest j2sdk 5.0 from java.sun.com.

In objective Java, the author once mentioned that defining constants in interfaces is a bad way to use them. We should always use interfaces to define types. However, in actual development work, many people still use interfaces in this way. The reason for doing so is that it is convenient to define constants. For example:
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 constants defined in these interfaces in your own classes, you must 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 ());
}
}
In this way, these constants become part of your class. If this class is not final, other classes inherit this class and make it inherit these constants, however, this may not be the result that the user needs.

In this case, we can define these constants in a class and use classname. variablename to access them. 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;
}
Currently, j2se 5.0 provides the static import function. You only need to write a static keyword after the import keyword to directly use the constants defined in the class. For example:
Import static staticex. irrationalconstants. sqrt_two;
Import static staticex. irrationalconstants. sqrt_three;
Import static staticex. transcendentalconstants. Pi;
Of course, you can also use the. * format, such
Import static staticex. irrationalconstants. *; 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 );
System. Out. println ("the sin of PI/4 is about" +
Sinpioverfour ());
}
}
Run this program and you will get
Pi is approximately 3.14159
The sin of PI/4 is about 0.707
I would like to remind you that if you use too many. * styles, the readability of the program may be negatively affected. It is difficult to see where the variable is defined.

This is the definition of constants and also applies to the use of static methods. For 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 irrationalconstants2 is imported into the constantswithstaticimport2 class, you can directly use its static method sinpioverfour.

The above briefly introduces how to use static import. You can refer to the connection for more information:
Http://java.sun.com/developer/JDCTechTips/2004/tt1005.html
Http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.