...
The code in this noncompliant example illegally exposes the (x,y) coordinates through the getPoint() method of the inner class. As a result, the AnotherClass class can illegally access the coordinates. Note that the fields x and y cannot be directly accessed (Coordinates.Point.x, for instance, is inaccessible) through the class Point at compile time. however, at runtime, these fields have package-private access.
| Code Block | ||
|---|---|---|
| ||
class Coordinates {
private int x;
private int y;
public class Point {
public void getPoint() {
System.out.println("(" + x + "," + y + ")");
}
}
}
class AnotherClass {
public static void main(String[] args) {
Coordinates c = new Coordinates();
Coordinates.Point p = c.new Point();
p.getPoint();
}
}
|
...