- Source: Static import
Static import is a feature introduced in the Java programming language that allows members (fields and methods) which have been scoped within their container class as public static, to be used in Java code without specifying the class in which the field has been defined. This feature was introduced into the language in version 5.0.
The feature provides a typesafe mechanism to include constants into code without having to reference the class that originally defined the field. It also helps to deprecate the practice of creating a constant interface (an interface that only defines constants then writing a class implementing that interface, which is considered an inappropriate use of interfaces.)
The mechanism can be used to reference individual members of a class:
or all the static members of a class:
For example, this class:
Can instead be written as:
Ambiguity
If two static members of the same name are imported from multiple different classes, the compiler will throw an error, as it will not be able to determine which member to use in the absence of class name qualification.
For example, the following code will fail to compile:
In this case, MAX_VALUE is ambiguous, as the MAX_VALUE field is an attribute of both java.lang.Integer and java.lang.Long. Prefixing the field with its class name will disambiguate the class from which MAX_VALUE is derived, but doing so makes the use of a static import redundant.
Notes
References
Static Import in Java 8 (Oracle)
Kata Kunci Pencarian:
- Java
- D (bahasa pemrograman)
- Java-gnome
- Fork bomb
- Selenium
- Layanan Pemanggilan Platform
- Layanan Panggilan Platform
- Hongdu JL-8
- Uni Afrika
- Pengujian unit
- Static import
- Java syntax
- MinGW
- Constant interface
- Mingw-w64
- Java Community Process
- Java (software platform)
- Dynamic-link library
- Comparison of C Sharp and Java
- Mockito