Differences between import static and import

Source: Internet
Author: User

Import static import is a new feature in jdk1.5. Generally, we import a class using import com ..... the static import is like this: Import static com ..... classname. *; here there is an extra static, and there is an additional class name after classname. *, which means to import static methods in this class. Of course, you can only import a static method. You just need to replace. * with the static method name. Then, in this class, you can directly call static methods with the method name, instead of using the classname. method name method.

The advantage of this method is that it can simplify some operations, such as the print operation system. out. println (...); you can write it into a static method print (...), print (...) you can.

However, we recommend that you use this method when there are many repeated calls. If there are only one or two calls, it is better to write them directly.

    Example:

In Java 5, the Import Statement is enhanced to provide even more powerful function to reduce the number of times of keys, although some people argue that this is at the cost of readability. This new feature becomes static import.

When you want to use static members, you can use static import (this feature can be used in both the API class and your own class ). The following is before and after the static ImportCodeInstance:

Before static import:

  1. Public ClassTeststatic {
  2. Public static void main (string [] ARGs) {
  3. System. Out. println (integer. max_value );
  4. System. Out. println (integer. tohexstring (42));
  5. }
  6. }
 
After static import:
 
  1. Import StaticJava. Lang. system. out;
  2. Import StaticJava. Lang. Integer .*;
  3.  
  4. Public class teststaticimport {
  5. Public static void main (string [] ARGs) {
  6. Out. println (max_value );
  7. Out. println (tohexstring (42));
  8. }
  9. }
 
Both classes generate the same output:
    1. 2147483647 
    2. 2a
 
Let's take a look at what will happen in the code using the static import feature:

1. Although this feature is usually called "static import", the syntax must be import static, followed by the fully qualified name or wildcard of the static member you want to import. In this example, we perform static import on the out object of the system class.

2. In this example, we may want to use several static members of the Java. Lang. Integer class. The static Import Statement uses wildcards to express "I want to perform static import on all static members in this class ".

3. Now we finally see the benefits of the static import feature! We do not need to type system in system. Out. println. Great! In addition, you do not need to type Integer in integer. max_value. Therefore, in this line of code, we can use shortcuts for static methods and a constant.

4. Finally, we will perform more quick operations. This time we will use the integer class method.

We are a bit ironic about this feature, but not just we are. We do not think that saving a small number of keys makes it difficult to read the code, but many developers require that it be added to the language.

The following are the principles for using static import:

    • You must say import static, not static import.
    • Beware of ambiguous names of static members. For example, if you perform static import on Integer and long classes, referencing max_value will lead to a compiler error because both integer and long have a max_value constant, and Java does not know which max_value you are referencing.
    • You can perform static import on static object references, constants (Remember, they are static or final), and static methods.

 

Import static member (static import)

After j2se 5.0, "Import static" is added. Its function is similar to the "import" introduced in package, so that you can save some typing effort, let the compiler do more things.

"Import static" is the syntax in use.ArticleWhen this function is introduced in the original book, most of them use static import to describe this function, and the compiler message is also written in this way. Here we still use static import as the description in the original article, to better demonstrate the role of this function, I call it "Import static member ".

Using the "Import static" syntax, You can import a class or a static member in the interface, for example, to see this hello! World! Program:

* Helloworld. Java

Import static java. Lang. system. out;

Public class helloworld {
Public static void main (string [] ARGs ){
Out. println ("Hello! World! ");
}
}

Here you will. lang. the out static member in the system category is imported into the program. When the compiler encounters an out name during compilation, it is automatically expanded to system. out, so this is the honey sugar (compiler suger) provided by the compiler ).

Let's take a look at an example. There are many static methods in the arrays category. For ease of use, you can use "Import static" to import these static methods into the program. For example:

* Useimportstatic. Java

Import static java. Lang. system. out;
Import static java. util. arrays. Sort;

Public class useimportstatic {
Public static void main (string [] ARGs ){
Int [] array = {2, 5, 3, 1, 7, 6, 8 };

Sort (array );

For (int I: array ){
Out. Print (I + "");
}
}
}

If you want to import all static members under the category, you can use the '*' character, for example:

* Useimportstatic. Java

Import static java. Lang. system .*;
Import static java. util. arrays .*;

Public class useimportstatic {
Public static void main (string [] ARGs ){
Int [] array = {2, 5, 3, 1, 7, 6, 8 };

Sort (array );

For (int I: array ){
Out. Print (I + "");
}
}
}

Like the import, the import static member (static import) function is for convenience, so that you can play less words, you give less words to the compiler to determine and automatically add for you, however, you must pay attention to name conflicts. Some name conflicts may be solved by the compiler in the following ways:

* Member Overwrite

If the category contains fields or method names with the same name, they are preferred.

* Region variable coverage

If there are variable names or arguments with the same name in the method, select them.

* Comparison on the overload method

For each static member using import static, if there is a conflict of the same name, try to use the overload mechanism to determine, that is, select an appropriate method by comparing the method name and the cited series.

If the compiler cannot judge, an error will be returned. For example, if the sort () method you have defined conflicts with the sort () method of arrays, And the compiler cannot identify the problem, the following message is displayed:
Reference to sort is ambiguous,

Both method sort (float []) in onlyfun. Caterpillar. arrays and

Method sort (float []) in Java. util. arrays match

In short, packages and categories can be used to manage resources to avoid conflicts with the same name, while "import" and "Import Staic" are the opposite, so that you can get some convenience, if a conflict of the same name occurs, this convenience is necessary.

 

 

Http://blog.sina.com.cn/s/blog_625651900100kwul.html

Http://hi.baidu.com/payapple/blog/item/8105941539ef380b4b90a710.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.