File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Explaining output regarding static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Explaining output regarding static methods" Watch "Explaining output regarding static methods" New topic
Author

Explaining output regarding static methods

Shanel Jacob
Ranch Hand

Joined: Jun 18, 2006
Posts: 112
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?

wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Technically, all parameters in Java are pass-by-value.
Are parameters passed by reference or passed by value in method invocation?
[ July 21, 2006: Message edited by: wise owen ]
Balaji Sampath
Ranch Hand

Joined: Sep 30, 2005
Posts: 63
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.

Thanks
Balaji.S
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729



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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Shanel Jacob
Ranch Hand

Joined: Jun 18, 2006
Posts: 112
Thank you for your explanation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Explaining output regarding static methods
 
Similar Threads
operator's
static block understanding
Variable change
Passing variable into methods
Increment Operator question