Differences between static import of Java package static import 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 a more static, and there is a more. * after the class name ClassName, which means to import the static method 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. This method can simplify some operations, such as printing System. out. println (...); You can write it into a static method print (...), Print (…) directly in use (...) You can. However, it is recommended to use this method when there are many repeated calls. If there are only one or two calls, it is better to directly write the example: in Java 5, the import Statement is enhanced, in order to provide even more powerful function to reduce the number of times, 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 an example of code before and after static import: Before static import: public class TestStatic {public static void main (String [] args) {System. out. println (Integer. MAX_VALUE); System. out. println (Integer. toHexString (42) ;}} after static import: import static java. lang. system. out; import static java. lang. integer. *; public class TestStaticImport {public static void main (String [] args) {out. println (MAX_VALUE); out. println (toHexString (42);} both classes generate the same output: 21474836472a 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, the fully qualified name or wildcard of the static member to be imported. 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. Below are several 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.

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.