Java Parameter Shadowing and this Keyword
Given: What is the result? - 
Community Votes
100% of anonymous learners picked answer C. Votes are pick records left by other test-takers — they are not the verified answer.
Community Insight
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.
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.
Community Discussion (4 comments)
Comments & Corrections
No comments yet — spotted an error or have a note? Share it below.
Expert Analysis
Why the Answer Is Correct
The code defines an instance variablex = 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 assumesthis.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 thatthis.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.