...
Noncompliant Code Example (Public Class)
In this This noncompliant code example concerns a class that is internal to a system, and not part of any public API. Nonetheless, the class Point is declared public. Even though this example complies with rule OBJ01-J. Declare data members as private and provide accessible wrapper methods, untrusted code could instantiate Point and invoke the public getPoint() to obtain the coordinates.
...
This compliant solution declares the Point class as package-private, in accordance with its status as not part of any public API.
| Code Block | ||
|---|---|---|
| ||
final class Point {
private final int x;
private final int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
public void getPoint() {
System.out.println("(" + x + "," + y + ")");
}
}
|
...
Noncompliant Code Example (Public Class With Public Static Method)
Again, this noncompliant code example concerns a class that is internal to a system, and not part of any public API. Nonetheless, the class Point is declared public. Even though this example complies with rule OBJ01-J. Declare data members as private and provide accessible wrapper methods, untrusted code could instantiate Point and invoke the public getPoint() to obtain the coordinates.
This noncompliant code example shows a public Point class that attempts to implement instance control using a private constructor. However, untrusted code may invoke the public static getPoint() method without instantiating the class because the class is public.
...