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