This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi the following code compiles fine. What I'm trying to understand is that why is it that when "x" is incremented to 1 in the "increment" method. It's value is 'lost' when trying to display "x" in the "main" method?
The basic concept is , when you declare the inner or local variable that has the same name has the class or instance variable then the inner variable shadows the outer variable. This concept is called shadowing of variables.
The problem is that the parameter int x in the increment method is hiding the static int x defined in the class. All changes to x in the increment method remain local to the method. If (within increment) you wish to modify the static int x's value you have to refer it using the class name: Incrementor.x [ July 21, 2006: Message edited by: Barry Gaunt ]