Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variable with Same Name

 
Arijit Ghosh
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does this code Compile ? Java is supposed to complain in case of identical variable names.
 
Keen Chen
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
the first i is class level while the 2th is the local level ( int the main method),
the 2th "shadow" the 1th.
got it?
 
ramki srini
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the scope of the variable is different in both case.
The first one will be the instance variable which you can access only through the instance of the class(unless its static variable).The second one will be within the scope of the Main method.
The compiler will complain only if you add duplicate instance variables or duplicate variable within the same scope (for ex. within a method).
I hope the above explanation is right.
 
shweta mathur
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, adding to what Ramki said, local variable, already declared in an enclosing block and therefore visible in a nested block, cannot be redeclared in a nested block. A local variable in a block can be redeclared in a block if the blocks are disjoint.
But this is not the case when the local & member variable have same name. Local variable shadows the member variable and takes priority.
Regards
Shweta
 
Arijit Ghosh
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi shweta,
Ok so that means the following code should not work because
int i declared in LOOP method is visible to SECONDLOOP and hence declaring int i again in SECONDLOOP should throw Compilation error.
I compiled the code using JDK 1.4 and it executed correctly with the following output --
Inside Loop method --> i : 1
Inside SecondLoop method --> i : 2


Explanation please ?
 
Anup Katariya
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arijit,
Every method has its own local memory available. It does not matter when same named local variable is used in different methods. But the following code will give u erros

I hope this helps
 
shweta mathur
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arjit,
In ur code, u have not created nested loop, rather u r calling an entirely different method & as Anup says every method has itz own local memory. Method secondloop() is not using the memory of loop(), so there will be no conflict.
Arjit the following article from Cindy Glass may also help clearing some of your doubts.
Not all Variables are created Equal - Cindy's Segment - a Whimsical View of the World
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic