The static import is a new feature of J2SE 5.0 that allows unqualified access to static members without inheriting from the type containing the static members. It allows you to refer to imported static members as if they were declared in the class that uses them. The class name and a dot (.) are not required to use an imported static member. A static import declaration has two forms – one that imports a particular static member and one that imports all static members of a class.
Static imports are of the following two types:
- One in which all the members of a particular static class are imported:
import static packageName.ClassName.*; - And the other in which only a particular member of a class is imported:
import static packageName.ClassName.membername;
key points: The syntax of an import statement must be import static followed by the fully qualified name of the static member to be imported.
An asterisk (*) sign must be used at the end of the declaration. The asterisk (*) sign specifies that all static members of the specified class would be available for use within the class declared in the file.
Responses to “Static Import statements”