[program-java] Re: Java: Precedence

  • From: "Sina Bahram" <sbahram@xxxxxxxxx>
  • To: <program-java@xxxxxxxxxxxxx>
  • Date: Tue, 7 Sep 2010 15:18:33 -0400

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: