File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 JavaScript Promises Essentials this week in the JavaScript 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: 19755
    
  20

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