6.6.4 Example: Access to public and Non-public Classes

If a class lacks the public modifier, access to the class declaration is limited to the package in which it is declared (§6.6). In the example:


package points;


public class Point {
	public int x, y;
	public void move(int dx, int dy) { x += dx; y += dy; }
}

class PointList { Point next, prev; }

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 Test {
	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 qualfied name, so that the simple name may be used thereafter:


package pointsUser;

import points.Point;


class Test {
	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 declared public and is therefore inaccessible outside package points.