[Java Study Notes] Static Import

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

1. Usage
Package tips;
Public interface constants {
Public static final int port_number = 90;
Public static final string app_name = "Dooly ";
Public static class staticnestedclass {}// subclass
}
By using import static, you can directly use static members without specifying the constants class name, including static methods.For example:
Import static tips. constants .*;
Public class staticimporttip {
Public static void main (string [] ARGs ){
System. Out. println (port_number );
System. Out. println (app_name );
System. Out. println (staticnestedclass. Class );
}
}

2. Notes
If you decide to use the static import feature, we recommend that you import constant names separately Instead of wildcards.This allows you to know the source of constants.
For example, in the following code snippet, a developer does not know which class the default_url and port_number are imported from after checking the code.
Import static tips. constants .*;//Note: Full path
Import static tips. gen. App .*;
Public class staticimporttip {
Public static void main (string [] ARGs ){
System. Out. println (port_number );
System. Out. println (default_url );
}
}
Assume that the import statement is as follows:
Import static tips. constants. port_number;
Import static tips. gen. App. default_url;
Then the constant source is obvious.

In addition,

● For a given package, you cannot use a single statement to statically import all class methods and class variables of all classes. That is to say, you cannot write code like this:

Import static java. Lang. *; // This does not compile!

● If a local method has the same name as a static import method, the local method is called.

Exercise caution when using static import. Static import makes the definition location of class methods and class variables blurred, making it more difficult to understand the code. The principle of using static import is: restrict the use of static import, and do not use static import in applications.

3. Why static import? What are the consequences and benefits?

Static import does not allow you to provide the class name. In this way, you can be a bit lazy, but unnecessary obfuscation is also introduced. Just as where createdate defines this issue. If a class you are writing needs to call multiple external class methods (dozens or more), you may have an excuse to use static import. However, a better way is to figure out why so many static calls are required. You may need to review the class design.

One possible reason for using static import is to simplify the call to multiple class constants, and these class constants are defined in one place. Assume that you have created multiple report classes and each report class needs to append the line break to the output. Therefore, each report class needs to use a common newline, just like the definition in rosterreporter:

Static final string newline = system. getproperty ("line. separator ");

You do not want to define this constant repeatedly in each report class. You can create a new class to hold constants. Later, it can hold other constants, such as the page width that applies to any report class.

Package sis. Report;

Public class reportconstant {

Public static final string newline =

System. getproperty ("line. separator ");

}

In a typical report class, since the constant newline is required in many places, you can add a static import so that your code looks clean:

Package sis. Report;

Import JUnit. Framework. testcase;

Import sis. studentinfo .*;

Import static sis. Report. reportconstant. newline;

 

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

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.