[J2SE 1.5] gradually excavation static import

xiaoxiao2021-03-06  95

.Article {Padding: 1em; Background: White;

.ABSTRACT {Padding: 1em 1EX 1EM 2EM;

.author, .date {padding: 0.5ex 0 0 0 0;}

H1.TITLE, H2.MAJOR, H3.MINOR, H3.LISTING {

MARGIN: 1EX 2PX 1EX; Color: # 0066cc; font-family: Sans-Serif;

}

H1.TITLE {margin-left: 0;} h3.Listing {margin: 1ex 2px 0;

H4.Label {

Margin: 0 -1EX 1EX; Padding: 4px; Border: 1PX Solid # 0066cc;

"background: # 0066cc;

}

Div.sidebar {

Margin: 1.5EM 0.5EX 1EM; Padding: 0 1EX;

Border: 2px solid # 0066cc; -Moz-Border-Radius: 8px;

}

H4.Label: first-child {

-Moz-Border-Radius-Topleft: 4px; -Moz-Border-Radius-TOPRight: 4px;

.code {

Margin: 0 0 1EX; Padding: 1em; Background: #ccccccc; font: monospace;

Border: 1px solid #ccccccc; -moz-border-radius: 8px;

}

. Subtitle {font-size: 80%; padding-left: 1ex;}

DT {font-weight: bold;} Li {margin-top: 1ex; margin-bottom: 1ex;}

P {margin: 1em 0;}. AtrCeth a {text-decoration: underline;

. at. at: # 0000f;}. at: # 6699ff;

.Article A: hover {color: # ff00; background: # fff99;}

Gradually excavate Static Import simpler access to static members

Sun Haitao (

Sun.haitao@126.com)

August 27, 2004

The "Static Import" mechanism is introduced in J2SE 1.5, which can use a static member using this mechanism to use a static member using the way the class or interface name. This article describes how this mechanism is used, as well as precautions during use.

In the Java program, it is not allowed to define a separate function and constant (of course, it is exactly, just finaled, only the variable can be assigned once). Even from their own functions, it is not necessary to attach something, but also find a class or interface as an interposed unit (you can hang a variety of members in the class, and the interface can only be used).

The way to do, is to put them with Static modifiers, define static members of this class or interface. The typical example of this is the Java.lang.math class-containing a large number of "functions such as SiN, COS" and "constants" such as Pi, E.

Traditionally, when accessing these functions, variables, and constants, you need to add the names of them in front. If it is just occasionally accessing these things, such a way of writing can work very well; but if you want to access these members, such a way of writing seems to be more commendable.

The "Static Import" mechanism is introduced in J2SE 1.5, which can use a static member using this mechanism to use a static member using the way the class or interface name.

"Static Import" or "Static Member Import"

The Static Import mechanism is often directly translated into "static import". But from the meanings, "static members import" is a more appropriate translation. However, considering that "static import" is relatively short, it is estimated that there will be a powerful vitality. Only include constant definitions

There is a variety of mounting units before the provision of constant: all constants are defined into an interface, and then make this interface that requires these constants (such an interface has a special name, called "constant interface" ).

This method can work. However, because of this, you can "infer" which interface "is" which interface you need to use "is" which constant needs to be used ", there is a question that will expose the implementation details.

Exactly imported way

The method of importing a static member is the beginning of the source file (before any class or the definition of the interface), plus similar statements:

Import Static Package Name. Class or Interface Name. Static member name;

Note that although the name of this mechanism is called "static import", the order here is just the opposite "Import Static".

Once imported, it can be accessed with the name of this member in the range of the entire source file.

Listing 1: Import SiN and Pi with an exact importance

// Accurate import math.sin and math.pi

Import static java.lang.math.sin;

Import static java.lang.math.pi;

Public class staticimportsamplea {

Public static void main (String [] args) {

System.out.println (sin (pi / 2)); // Output "1.0"

}

}

Special issues of free classes and interfaces outside the package

The Java language does not require each class and interfaces must belong to a package. However, after J2SE 1.4, whether it is the Import statement, or the import static statement is also required to be named. In other words, for classes and interfaces that do not belong to any package, it is neither imported into itself, nor can Import Static into its static member.

2. Methods to import on demand

The Static Import mechanism also supports an import approach that does not have to point out static member names one by one. At this time, use such syntax:

Import static package name. Class or interface name. *;

Note that this way means that when you encounter an unknown member, you can find all the static members of this class or interface into this class or interface.

Listing 2: Import SiN and Pi with ways imported on demand

// Declare when you encounter an unknown member to java.lang.math to find

Import static java.lang.math. *;

Public class staticImportsampleb {

Public static void main (String [] args) {

System.out.println (sin (pi / 2)); // Output "1.0"

}

}

3. All kinds of things that can be imported

With the import static statement, you can import everything in a class, which is modified by static, including variables, constants, methods, and inside.

Listing 3: Variables, constants, methods and internal categories can be imported

Package com.example.p3;

Public class staticimportee {

Public static int one = 1;

Public Static Final Int Two = 2;

Public static int three () {

Return 3;

}

Public static class four {public int value () {) {PUBLIC INT VALUE

Return 4;

}

}

}

Package com.example.p3;

Import static com.example.p3.staticimportee. *;

Public class staticimporter {

Public static void main (String [] args) {

System.out.println (one);

System.out.println (TWO);

System.out.println (three ());

System.out.println (New Four ());

}

}

Unaffected access control

Static import cannot break through the limitations of the original access control mechanism in the Java language, but not new constraints in this regard. It has been a static member who has permission access can be imported and used; and a static member who has no permission access is still accessible after using this method.

4. Conflict between imports

Different classes (interfaces) can include the same static member of the name. Therefore, when Static Import is performed, the "two statements import the same name" is possible.

At this time, J2SE 1.5 will handle this:

If both statements are in the form of precise imports, or they are imported in need, then compile errors. If a statement uses an exact import form, a form that is imported in need is valid in the form of precise imported form.

Note that if the two as the static member of the same name is an attribute, and the other is a method, since there is a difference in the use of the writing, it will not cause any conflict.

Listing 4: One effective in the form of precise import

Package com.example.p4;

Import static com.example.p4.importee1.name;

Import static com.example.p4.importee1. *;

Import static com.example.p4.importee2. *;

Import static com.example.p4.importee2.pass;

PUBLIC Class Importer {

Public static void main (String [] args) {

System.out.println (Name); // Output "Name1"

System.out.println (pass); // Output "Pass2"

}

}

Package com.example.p4;

Public class importee1 {

Public static string name = "name1";

Public static string pass = "pass1";

}

Package com.example.p4;

Public class importee2 {

Public static string name = "name2";

Public static string pass = "pass2";

}

5. Local and foreign competition

Sometimes, imported things may also conflict with local things, this situation is "local priority".

Listing 5: Local priority

Package com.example.p5;

Import static com.example.zero. *;

Public class one {

Public static int flag = 1;

Public static void main (string [] args) {system.out.println (flag); // output "1"

}

}

Package com.example.p5;

Public class zero {

Public static int flag = 0;

}

6. Continue to Static Import

During the compilation, all the names that simplified due to Static Import will be reset by the compiler. So in terms of performance, Static Import has no effect. But name simplification may cause some maintenance issues.

Remove the type name in front of the static member, it will help to make simple in frequent calls, but at the same time, it also lost the prompt information about "where this thing is defined", adding the trouble of reading understanding. If the imported source is very famous (such as java.lang.math), or less from the total number of sources, this problem is not serious; but if it is not part of these, this is not a problem that can be ignored.

7. Summary

With a STATIC Import mechanism provided in J2SE 1.5, you can use a simpler way to access static members of the class and interfaces. However, using this mechanism is not no price, it may have a certain trouble to maintain the maintenance work when using improper use. Therefore, it is necessary to do some of the trade-offs before specific use.

Reference resource

You can find the SDK of the J2SE 1.5 and its documentation by Sun's Java Technology page. The latest version is currently J2SDK 1.5 Beta 2. Note When using this version of Javac, add "-Source 1.5" as a parameter to compile the source code for the new language characteristics in J2SE 1.5. John Zukowski introduced how to start using J2SDK 1.5 basics in "Tam Tiger: Tiger Preview". However, this article is written in accordance with the status of J2SDK 1.5 Alpha version, so some details (such as download address and default installation path) have changed. "JSR 201: Extending The Java Programming Language with Enumeration, Autoboxing, Enhanced for loops and static import" defines a lot of new language features in J2SE 1.5, including "static import". Joshua Bloch Chapter 4, "Classes and Interfaces" in the "Effective Java" book, explains the issues that exist only the interfaces of the constant. Calvin Austin In the article "J2SE 1.5 in a nutshell", Calvin Austin has a comprehensive and summary introduction to the new features in J2SE 1.5.

转载请注明原文地址:https://www.9cbs.com/read-122778.html

New Post(0)