• Post Reply Bookmark Topic Watch Topic
  • New Topic

local and class variable values  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im a little confused

in a video i was watching on methods,local and class variables etc

he has a similar class setup like this:



and he was explaining that class variables can be overwritten in methods,but that is only local
then he prints out the variable in main again after the method call and sure enough its the original value that was assigned
i tried to recreate this,but it doesnt seem to be working the same,the class variable remains changed by the local assignment even if i print it out again in main
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post the output you are talking about and add some comments to it that describes what you expected vs what happened?
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could it be that line 13 was like:
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:. . . in a video i was watching . . .
Please always tell us where such information comes from, to prevent copyright issues and so we can maybe assess the quality of the source,
he has a similar class setup like this:

. . .
I hope you have copied that wrongly because that is dreadful code. It doesn't even show variable shadowing.Lines 7 and 22 show the instance field correctly disambiguated from the parameter. Line 12 shows the parameter shadowing the field, and running such code on Eclipse will produce a warning about assignment with no effect. Line 17 shows a newly‑declared local variable shadowing the field, similar to what PS showed.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all for the help...

yes piet it seems thats what i had different

now it prints
50
100
50

but i still dont really understand how this works regarding local/global variable values...is it just that int myNumber is its own variable in the local space
before i was calling the class variable in the local space? not really sure

thanks

ps"campbell im waching derek banas tutorials on youtube,is it ok to put links etc...and should i with time etc?"
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:. . . local/global variable values... . . .
You do not have any global variables there. You have local variables and fields
ps"campbell im waching derek banas tutorials on youtube,is it ok to put links etc...and should i with time etc?"
Yes, you shou‍ld quote the link: for times you simply say 12min 34sec.

Search for local variable shadowing Java. Try the Java® Language Specification (beware: that can be very difficult to read).
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:ps"campbell im waching derek banas tutorials on youtube,is it ok to put links etc...and should i with time etc?"


Actually it would be preferable to just copy the code you're asking about and paste it here. You can copy code from video tutorials, can't you?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:. . . You can copy code from video tutorials, can't you?
Depends on the tutorial.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jon,

and,  gotten any wiser yet with respect to local variables and shadowing of member fields?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!