Java static import (InPort static) detailed

Source: Internet
Author: User

In Java 5, import statements have been enhanced to provide even more powerful keystrokes, although some argue that this is at the expense of readability. This new feature becomes static import.

When you want to use a static member, you can use this feature in both the class in the API and on your own class. The following is a code instance before and after a static import:

Before a static import:

    1. public class Teststatic {
    2. public static void Main (string[] args) {
    3. System.out.println (Integer.max_value);
    4. System.out.println (integer.tohexstring (42));
    5. }
    6. }

After a static import:


Import static java.lang.System.out;
    1. Import static java.lang.integer.*;
    2. public class Teststaticimport {
    3. public static void Main (string[] args) {
    4. Out.println (Max_value);
    5. Out.println (tohexstring (42));
    6. }
    7. }
All two classes produce the same output:
    1. 2147483647
    2. 2a
Let's take a look at what happens in the code that uses the static import feature:

1, although this feature is commonly referred to as "static import", the syntax must be import static, followed by the fully qualified name of the static member you want to import, or a wildcard. In this example, we do a static import on the Out object of the system class.

2. In this example, we might want to use several static members of the Java.lang.Integer class. The static import statement uses wildcards to express "I want to do a static import on all static members in this class."

3, now we finally see the benefits of static import features! We do not have to type system in SYSTEM.OUT.PRINTLN. That's great! In addition, we do not have to type an integer in Integer.max_value. So, in this line of code, we can use shortcuts for static methods and a constant.

4, finally, we do more quick operation, this time for the method of the integer class.

We have a little bit of irony about this feature, but not just us. We don't think that saving a small number of keystrokes can make the code harder to read, but many developers require it to be added to the language.

Here are a few guidelines for using static imports:

    • You must say import static, cannot say static import.
    • Beware of ambiguous named static members. For example, if you perform a static import on an integer class and a long class, referencing Max_value will cause a compiler error because both the integer and long have a Max_value constant, and Java will not know which max_value you are referencing.
    • You can statically import on static object references, constants (remember, they are static or final), and the static method.

Java static import (InPort static) detailed

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.