Accessing Java Classes in the Default Package from Named Packages
In a collaborative Grails project, accessing a Groovy-created object in the default package from a Java class in a named package can pose a challenge.
Question: Is there a way to access this default package object from within a packaged Java class?
Answer:
Accessing classes in the default package from named packages is generally not possible.
Prior to Java 2 Standard Edition (J2SE) 1.4, it was permissible to import classes from the default package using the syntax import Unfinished;. However, this practice is no longer allowed.
To access a default package class from a packaged class, you must move the default package class into a named package. This involves:
Note: In Java Development Kit (JDK) 7 and 8, importing from unnamed packages is strictly prohibited (bug 6975015). The following condition must be met for successful compilation:
Background: The default package exists for convenience in developing small, temporary, or initial applications. Accessing default package classes from named packages goes against the design principles of encapsulation and modularity.
The above is the detailed content of How Can I Access a Default Package Object from a Named Java Package?. For more information, please follow other related articles on the PHP Chinese website!