Java Parameter Shadowing and this Keyword
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

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?
What is the purpose of this.x?
Community Votes
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
-
Tojose 👍 4the right answer is C. 1001 100 1000
-
james2033 👍 3 Answered Cpackage 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
-
xplorerpj 👍 1Right answer is C this.x refers to publicly declared/initialized variable
-
SrinivasJasti 👍 1 Answered Cthis.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.