Static import of new features of j2se5.0

Source: Internet
Author: User
Before j2se5.0, if you need to use a static method of another class, such as a method in Java.lang.Math, you need to write the following code:

Double value = Math.log (*MATH.PI);



Now you just have to import statically, and then you can use static methods and static fields directly in your code:

Import static java.lang.math.*;
...

Double value = log (*PI);





See a detailed example. Let's first build a class that contains a static method and a static field:
PackageCom.kuaff.jdk5; Public classStaticclass { Public Static StringLABEL = "Chinese Ancient famous sword"; Public Static voidPrintlist () { String[] Swords = New String[] {"Xuan Yu Jian", "Zhan Lu", "Red Xiao Xiao", "Too Ah", "Yongyeon", "Gan", "Moye", "Fish sausage", "pure June", "Bearing Shadow"}; for( StringName:swords) { System. out.printf ("%s%n", name); }     } }
Create a new class, statically import the fields and methods above, and use them:
PackageCom.kuaff.jdk5; Import Staticcom.kuaff.jdk5.staticclass.*;//Static Import Public classStaticimportshow { Public voidTestsi () { System. out.printf ("%s:%n", LABEL);     Printlist (); } Public Static voidMain String[] args) {Staticimportshow show = NewStaticimportshow ();     Show.testsi (); } }

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.