This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variable shadowing

 
Rajeswari Kumar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
340 is decalred as final. Then how we can increment that
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yep..!! got it.!
Thanks.!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic