Static import in Java 5

Source: Internet
Author: User

Sometimes we may need to write many statements similar to system. Out. println () in the class. Every time we write so much trouble, with Java 5, this becomes very simple. Let's take a look at the static import example:

Package Com. Kay. java5.test1;

Import StaticJava. Lang. system. out;//Static import of Java 5
Import StaticJava. Lang. system. Err;

Public ClassStaticimportclass {

Public Static VoidMain (string [] ARGs ){
Out. println ("Hello, world");
Err. println ("Error");
}
}

We can also use static import methods:PackageCom. Kay. java5.test1;

Import StaticJava. Lang. system. out;//Static import of Java 5
Import StaticJava. util. arrays. Sort;//Imported the static method sort () in the arrays class ()

ImportJava. util. arrays;

Public ClassStaticimportclass {

Public static void main (string [] ARGs) {

int [] I = { 3 , 1 , 2 };
// sort method call
sort (I);
out. print (arrays. tostring (I);

}< BR >}

sometimes we need to import a bunch of static methods in a class. If one write is too tired, we can use the wildcard in static import: package COM. kay. java5.test1;

Import static JAVA. lang. system. out; // static import of Java 5
Import static JAVA. lang. math. * ; // static import wildcard

Public class staticimportclass {

Public Static VoidMain (string [] ARGs ){

Out. Print (PI );

}
}

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.