Hello, Jim and all. The Java compiler will not allow you to redeclare a local variable having the same identifier value as the instance variable identifier as you have supplied in your example. Although C++ will allow this sort of scope hiding, Java will not.
Thanks & kind regards, Jef----- Original Message ----- From: "Sina Bahram" <sbahram@xxxxxxxxx>
To: <program-java@xxxxxxxxxxxxx> Sent: Tuesday, September 07, 2010 4:15 PM Subject: [program-java] Re: Java: Precedence Jim,My appologies, no slap down intended. Sorry, was probably far too Socratic in my response.
Care to post the whole code? Take care, Sina -----Original Message-----From: program-java-bounce@xxxxxxxxxxxxx [mailto:program-java-bounce@xxxxxxxxxxxxx] On Behalf Of Corbett, James
Sent: Tuesday, September 07, 2010 3:53 PM To: 'program-java@xxxxxxxxxxxxx' Subject: [program-java] Re: Java: Precedence Hi:Obviously zero. ...Ok, the reason I ask was because in this piece of code that I've inherited x for lack of a better name is used in at least 30 places in the class and the class level x is declared as transient. I'm trying to figure this mess out and thought I
would ask the initial question with out the slap down. Jim -----Original Message-----From: program-java-bounce@xxxxxxxxxxxxx [mailto:program-java-bounce@xxxxxxxxxxxxx] On Behalf Of Sina Bahram
Sent: September 7, 2010 15:19 To: program-java@xxxxxxxxxxxxx Subject: [program-java] Re: Java: Precedence Jim,You should be able to answer this question yourself based on first principles.
Let's examine what you have.You have a method, and in there you declare a variable named x. absolutely nothing else matters, even a million other definitions of x in a million other scopes, because the next line returns x, and the x in the immediate scope is the variable you just defined;
thus, 11 is returned.Also, don't do x=0 when defining your variable. if you want to assign a value to x, then do it in your constructor. Furthermore, x
is an int, which means it defaults to 0.What if we change that line to "return this.x;"? Can you tell me what value is returned?
Take care, Sina ________________________________From: program-java-bounce@xxxxxxxxxxxxx [mailto:program-java-bounce@xxxxxxxxxxxxx] On Behalf Of Corbett, James
Sent: Tuesday, September 07, 2010 2:21 PM To: 'program-java@xxxxxxxxxxxxx' Subject: [program-java] Java: Precedence Hello all: I know y'all say don't do that but let's just pretend. If a member is declared private to the class such as Public Class MYClass { Private int x = 0; Public MYClass() { } Public int getX () { int x = 11; return x; } } What would you expect to have returned as a value of .getX()? Jim James M. Corbett Programmer / Analyst | Canada Revenue Agency | Agence du revenue du Canada 875 Heron Rd. Ottawa, On. K1A0L5 James.Corbett@xxxxxxxxxxxxx Telephone | Téléphone: (613) 941-1338 Facsimile | Télécopieur: (613) 941-2261 Government of Canada | Gouvernement du Canada