Java Key Technology (1) static import of syntax

Source: Internet
Author: User

1.1.Static Import

Static Import (Import Static) is the syntax for a static member of an imported class that is deprecated. With respect to normal imports, there is a benefit to static importing that you can have fewer words and not write class names.

There is a class that is imported:

/*** @Title: helloworld.java* @Package com.test.javatechnology.staticimport* @Description:*@author http://www.cnblogs.com/coe2coe/* @date March 19, 2017 pm 8:03:24*@versionV1.0*/  Packagecom.test.javatechnology.staticimport.si;/*** @ClassName: helloworld* @Description:*@author http://www.cnblogs.com/coe2coe/* @date March 19, 2017 pm 8:03:24**/ Public classHelloWorld { Public Static voidHello () {System.out.println ("Hello");}  Public Static voidsay () {System.out.println ("Say");}} 

Now use the normal import method to import the class and invoke two static methods:

/*** @Title: staticimporttest.java* @Package com.test.javatechnology.staticimport* @Description:*@author http://www.cnblogs.com/coe2coe/* @date March 19, 2017 pm 8:05:44*@versionV1.0*/ PackageCom.test.javatechnology.staticimport;ImportCom.test.javatechnology.staticimport.si.HelloWorld;/*** @ClassName: staticimporttest* @Description:*@author http://www.cnblogs.com/coe2coe/* @date March 19, 2017 pm 8:05:44**/ Public classStaticimporttest {/** * @paramargs*/ Public Static voidMain (string[] args) {Helloworld.hello (); Helloworld.say (); } }

How to use static import:

Import  Static  com.test.javatechnology.staticimport.si.helloworld.*;

used here * import all static and static variables, or you can use specific member names to import only a specific method statically.

The class name cannot be written at this time, or a compilation error occurs.

Helloworld.hello (); Helloworld.say ();

The correct wording is as follows:

Hello (); say ();

The disadvantage of static import is that it is possible to create a name conflict, which reduces the understandable nature of the code.

The complete code looks like this:

/*** @Title: staticimporttest.java* @Package com.test.javatechnology.staticimport* @Description:*@author http://www.cnblogs.com/coe2coe/* @date March 19, 2017 pm 8:05:44*@versionV1.0*/ PackageCom.test.javatechnology.staticimport;Import Staticcom.test.javatechnology.staticimport.si.helloworld.*; /*** @ClassName: staticimporttest* @Description:*@author http://www.cnblogs.com/coe2coe/* @date March 19, 2017 pm 8:05:44**/ Public classStaticimporttest {/** * @paramargs*/ Public Static voidMain (string[] args) {hello (); Say ();}}

Java Key Technology (1) static import of syntax

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.