To use static members (methods and variables) We must give the class that provides this static member.
Static import allows static variables and static methods of the imported class to be directly visible in the current class , using these static members without giving their class names.
Static import is also a new feature introduced by JDK5.0, and the following examples illustrate the use of static imports:
For example, define a class in a package first:
Package Com.example.learnjava; Class common{ int age = tenvoid output () {System.out.println ("Hello world!" ); }}
when used in another package , this is used if you do not use a static import:
Package com.example.learnjava2; Import Com.example.learnjava.Common; Classvoidint a = common.age; System.out.println (a); Common.output (); }}
The import statement was added earlier, the common class is imported , and the static member variable and static method are used to add the class name .
using static import
The syntax for static import is:
Import static package name. class name. static member variable;
Import static package name. class name. static member function;
Note the member variables and method names are imported.
After using static import as the previous program:
Package com.example.learnjava2; Static Com.example.learnjava.Common.AGE; Static Com.example.learnjava.Common.output; Classvoidint a = age ; System.out.println (a); Output (); }}
Disadvantages
Excessive use of static imports can reduce the readability of your code to some extent.
Former Wensheng Knight Wind
Original address: http://www.cnblogs.com/mengdd/archive/2013/01/23/2873312.html
"Go" Java static import Imports Static