| Author |
Variable shadowing
|
Rajeswari Kumar
Greenhorn
Joined: Mar 23, 2007
Posts: 26
|
|
class Knowing { static final long tooth = 343L; static long doIt(long tooth) { System.out.print(++tooth + " "); return ++tooth; } public static void main(String[] args) { System.out.print(tooth + " "); final long tooth = 340L; new Knowing().doIt(tooth); System.out.println(tooth); } } Output is 343 341 340 I understood how 343 has come. It printed the instance variable Can anybody explain about the other two. Which version of tooth variable is passed thru the doIt method. Please help me
|
 |
Srinivas Kumar
Ranch Hand
Joined: Jul 14, 2005
Posts: 52
|
|
class Knowing { static final long tooth = 343L; static long doIt(long tooth) { System.out.print(++tooth + " "); return ++tooth; } public static void main(String[] args) { System.out.print(tooth + " "); final long tooth = 340L; new Knowing().doIt(tooth); System.out.println(tooth); } } Output is 343 341 340 Always local variable is of high priority than that of instance or class level variables. So 340 gets passed to the method and gets incremented by 1 in doIt() method and prints it. In java, variables are passed by value. So the value of tooth in main will not get reflected (Also note that it is a final variable).So finally 340 gets printed.
|
 |
Rajeswari Kumar
Greenhorn
Joined: Mar 23, 2007
Posts: 26
|
|
|
340 is decalred as final. Then how we can increment that
|
 |
anil kumar
Ranch Hand
Joined: Feb 23, 2007
Posts: 447
|
|
Hi static long doIt(long tooth) { System.out.print(++tooth + " "); return ++tooth; } when you pass some value to this method a local variable of "tooth" is created.And if we do any operations on this variable the one who called this mehtod with a variable is not effected ie(From main your are calling this method with final variable is not effected). I think you got it
|
 |
Rajeswari Kumar
Greenhorn
Joined: Mar 23, 2007
Posts: 26
|
|
|
Okay okay.Since a final variable is passed as a parameter,What I thought is locally created tooth variable must also be of type final
|
 |
Lovleen Gupta
Ranch Hand
Joined: Feb 26, 2007
Posts: 63
|
|
I've a question - In the first print statement : System.out.print(tooth + " "); why did it take the instance variable tooth when you say that local variable has higher priority than instance variable? Thanks.
|
 |
anil kumar
Ranch Hand
Joined: Feb 23, 2007
Posts: 447
|
|
Hi Lovleen Gupta public static void main(String[] args) { System.out.print(tooth + " ");//1 st print final long tooth = 340L; new Knowing().doIt(tooth); System.out.println(tooth); } I think you are speaking about this 1st print statement. 1) here one important point tooth is not INSTANCE VARIABLE.It is a class varaible. Here the local variable is not declared before the first print statement. If it is declared before then the output is 340L 2) java won't consider Forward referencing. I think you have understood.
|
 |
Lovleen Gupta
Ranch Hand
Joined: Feb 26, 2007
Posts: 63
|
|
yep..!! got it.! Thanks.!!
|
 |
 |
|
|
subject: Variable shadowing
|
|
|