Java Parameter Shadowing and this Keyword

1Z0-829 By islever Editorial Last updated

Short answer

C The output is 1001, 100, 1000 because the local parameter x is incremented but the instance variable this.x remains 100.

Question

Given: What is the result? - Exhibit: Java Parameter Shadowing and this Keyword
  1. 100
  2. 101
  3. 1001 Correct Answer
  4. 1001

Key Takeaways

Tests understanding of scope: when a method parameter shares a name with an instance variable, 'this.x' accesses the instance field while 'x' accesses the parameter.

This question tests Java's parameter shadowing rules where a local variable hides an instance field. The correct answer is C because the final println outputs the unchanged local variable value.

Common mistake: Learners often select B (101) by confusing 'this.x' with the modified local variable x, or D (1001) by assuming the print order differs from the code flow.

Expert Analysis

Why the Answer Is Correct

The code defines an instance variable x = 100 and a local variable x = 1000. In myMethod, the parameter int x shadows the instance variable. The statement x++ increments the local parameter to 1001. System.out.println(x) prints 1001. Crucially, this.x explicitly refers to the instance variable, which remains unmodified at 100. Finally, System.out.println(x) in main prints the original local variable value 1000. The combined output matches option C.

Why the Other Options Are Wrong

Option A (100) ignores the increment on the local variable. Option B (101) incorrectly assumes this.x was incremented or that the second print refers to the instance variable after modification. Option D (1001) typically results from misreading the final print statement as printing the instance variable or misunderstanding the flow.

Community Comment Notes

Users like Tojose and james2033 confirm the output sequence: 1001, then 100, then 1000. SrinivasJasti highlights the distinction between the instance variable and local variable. xplorerpj notes that this.x refers to the publicly declared variable, reinforcing the shadowing concept.

Exam Strategy

Always distinguish between member variables and local parameters. Use the 'this' keyword to access the current object's fields when they are shadowed by local names.

Frequently Asked Questions

Why does x++ not affect this.x?

Because x++ modifies the local parameter variable, which shadows the instance variable. The instance variable is untouched.

What is the purpose of this.x?

It explicitly references the instance field of the current object, bypassing any local variable with the same name.

More 1Z0-829 FAQ →

Community Votes

C
100%

100% of anonymous learners picked answer C. Votes are pick records left by other test-takers — they are not the verified answer.

Discussion 4 contributions

Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.

From public community threads 4

  1. Tojose 👍 4
    the right answer is C. 1001 100 1000
  2. james2033 👍 3 Answered C
    package q23; public class App { public int x = 100; public static void main(String[] args) { int x = 1000; App t = new App(); t.myMethod(x); System.out.println(x); } public void myMethod(int x) { x++; System.out.println(x); System.out.println(this.x); } } // Result: // 1001 // 100 // 1000
  3. xplorerpj 👍 1
    Right answer is C this.x refers to publicly declared/initialized variable
  4. SrinivasJasti 👍 1 Answered C
    this.x is instance variable and int x = 1000 is local variable

Reader comments & corrections

No reader corrections yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

← Back to 1Z0-829 Study Guide