캡슐화의 이점을 제공하지 못하는 클래스

class Point{
	public double x;
	public double y;
}

패키지 바깥에서 접근할 수 있는 클래스라면 접근자를 제공하자

public class Point {
    public double x;
    public double y;

    public Point(final double x, final double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public void setX(final double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(final double y) {
        this.y = y;
    }
}

package-private 클래스 혹은 private 중첩 클래스 데이터 캡슐화

public class ColorPoint {
    private static class Point{
        public double x;
        public double y;
    }

    public Point getPoint(){ 
        Point point = new Point(); 
        point.x = 1.2; 
        point.y = 5.3; 

        return point; 
    }
}

정리