Objects: Instances of Classes

AP Computer Science A· difficulty 3/5

public class Holder {
    private int n;
    public Holder(int n) { this.n = n; }
    public int getN() { return n; }
    public void setN(int n) { this.n = n; }
}
// ...
Holder a = new Holder(1);
Holder b = a;
b.setN(42);
System.out.println(a.getN());

What is printed?

  • A

    0

  • B

    1

  • C

    42

    check_circle
  • D

    Compile-time error

Explanation

a and b refer to the same object; modifying via b is visible via a.

Want 10 more like this — adaptive to your weak spots?

Related questions