[program-java] Re: Java: Precedence

  • From: "qubit" <lauraeaves@xxxxxxxxx>
  • To: <program-java@xxxxxxxxxxxxx>
  • Date: Tue, 7 Sep 2010 20:41:42 -0500

I can think of lots of times a variable, such as x or i as an iterator, 
might be used inside methods over and over again in different scopes.
Your example is a little code fragment, but in practice, a method could get 
more involved.
But of course, OO style is supposed to prevent a lot of that.
--le
----- Original Message ----- 
From: "Jeffrey Fidler" <jeffrey.fidler@xxxxxxxxxxx>
To: <program-java@xxxxxxxxxxxxx>
Sent: Tuesday, September 07, 2010 4:19 PM
Subject: [program-java] Re: Java: Precedence


I stand corrected!  I even went as far as creating a second method that
declared another local variable with the same identifier and type as in my
first method and this compiled and ran without a hitch.  What the compiler
will definitely not let us do is to redeclare a local variable within the
same method scope.

I'm not sure why anybody would want to use the same identifier in several
different local scopes?  And, Sina, thank you for pointing out the extremely
obvious case of instance identifiers and local idenfifiers in the case of
our accessor methods!




----- Original Message ----- 
From: "Sina Bahram" <sbahram@xxxxxxxxx>
To: <program-java@xxxxxxxxxxxxx>
Sent: Tuesday, September 07, 2010 4:54 PM
Subject: [program-java] Re: Java: Precedence


I hate to disagree, but it absolutely will. I just compiled and tested a
solution to verrify what I thought I already knew.

You can shadow variables all you like.

For a conceptual aid, think of how setters work. Often time, the argument
name is the same as the inner name, leading to code like
this.


public void setAge(int age) {
this.age = age;
}

Take care,
Sina



-----Original Message-----
From: program-java-bounce@xxxxxxxxxxxxx
[mailto:program-java-bounce@xxxxxxxxxxxxx] On Behalf Of Jeffrey Fidler
Sent: Tuesday, September 07, 2010 4:30 PM
To: program-java@xxxxxxxxxxxxx
Subject: [program-java] Re: Java: Precedence

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













Other related posts: