Java Reference
In-Depth Information
two classes are declared in the compilation unit. The class
Point
is available outside the
package
points
, while the class
PointList
is available for access only within the package.
Thus a compilation unit in another package can access
points.Point
, either by using its
fully qualified name:
package pointsUser;
class Test1 {
public static void main(String[] args) {
points.Point p = new points.Point();
System.out.println(p.x + " " + p.y);
}
}
or by using a single-type-import declaration (ยง
7.5.1
) that mentions the fully qualified
name, so that the simple name may be used thereafter:
package pointsUser;
import points.Point;
class Test2 {
public static void main(String[] args) {
Point p = new Point();
System.out.println(p.x + " " + p.y);
}
}
However, this compilation unit cannot use or import
points.PointList
, which is not de-
clared
public
and is therefore inaccessible outside package
points
.
Example 6.6-4. Access to Default-Access Fields, Methods, and Constructors
If none of the access modifiers
public
,
protected
, or
private
are specified, a class member
or constructor is accessible throughout the package that contains the declaration of the
class in which the class member is declared, but the class member or constructor is
not accessible in any other package.
If a
public
class has a method or constructor with default access, then this method or
constructor is not accessible to or inherited by a subclass declared outside this pack-
age.
For example, if we have: