A summary of static import usage in Java _java

Source: Internet
Author: User

Change the job to the Java pick up again, the language used in the university, although not complicated, there are some strange places. Like static import.

The Static import is a feature introduced in JDK 1.5, but I didn't notice it in college. Its function is to import a static method or a constant. Like what:

Import static java.lang.math.*;
 
public class HelloWorld {public
 static void Main (string[] args) {
  System.out.println ("Hello world!");
  System.out.println ("Considering a circle with a diameter of 5 cm, it has:");
  System.out.println ("A Circumference of" + (Math.PI * 5) + "cm");
  System.out.println ("and an area of" + (Math.PI * MATH.POW (2.5,2)) + "sq. cm");
 }

After you use the static import, you can write:

Import static java.lang.math.*;
Import static java.lang.System.out;
 
public class HelloWorld {public
 static void Main (string[] args) {
  out.println ("Hello world!");
  Out.println ("Considering a circle with a diameter of 5 cm, it has:");
  Out.println ("A Circumference of" + (PI * 5) + "cm");
  Out.println ("and an area of" + (PI * POW (2.5,2)) + "sq. cm");
 }

Note "Math." and "System." It can be omitted.

The rules for Static import and import are similar, and the referenced content cannot be ambiguous.

With the static import, the code becomes shorter and more readable, but to some extent it makes it difficult to understand the whole code, because constants and static methods look like global and global methods, a bit of C + +, and lose some of the beauty of OO.

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.