Reading another object's private field

Consider the following class:


Java
public class Point {
    private int x;
    private int y;

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

    public boolean sameAs(Point p) {
        return x == p.x && y == p.y;
    }
}


A separate class runs System.out.println(new Point(2, 3).sameAs(new Point(2, 3)));. What happens?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Access Modifiers Questions: