This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Method Variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method Variable" Watch "Method Variable" New topic
Author

Method Variable

david john
Greenhorn

Joined: Oct 16, 2008
Posts: 16
Hi, In the below mentioned code, value of x and y is printed as 3 and 7. I have a doubt, as I understand scope of variable used in the method is only till the method exists. Once the method is finished variable used in method will also not exist. Why in this case method variable is overwritting the class variable?



Thanks in advance for your help!!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

If you indent your code properly you will immediately see why:

Now look closely at the print statement: which x and y does it actually print?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
amitabh mehra
Ranch Hand

Joined: Dec 05, 2006
Posts: 98
david john wrote:... I have a doubt, as I understand scope of variable used in the method is only till the method exists. Once the method is finished variable used in method will also not exist...


You yourself have answered it
When you are printing the x and y in the main method, its the local variables not the other ones.
david john
Greenhorn

Joined: Oct 16, 2008
Posts: 16
Silly of me.... Did not see that... thanks a lot...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method Variable
 
Similar Threads
how the program works?
static variable
what layouts to use
Running a thread's run() method after the thread has completed.
Dan's questions : static variable