this Keyword

AP Computer Science A· difficulty 3/5

public class Demo {
    private int x = 10;
    public int compute() {
        int x = 5;
        return this.x + x;
    }
}
// ...
Demo d = new Demo();
System.out.println(d.compute());

What is printed?

  • A

    20

  • B

    15

    check_circle
  • C

    10

  • D

    5

Explanation

this.x refers to the instance variable (10) and x refers to the local (5); the sum is 15.

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

Related questions