• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method Variable

 
david john
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
amitabh mehra
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Silly of me.... Did not see that... thanks a lot...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic