File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Variable with Same Name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Variable with Same Name" Watch "Variable with Same Name" New topic
Author

Variable with Same Name

Arijit Ghosh
Ranch Hand

Joined: Feb 01, 2002
Posts: 174
How does this code Compile ? Java is supposed to complain in case of identical variable names.


Regards,<br /> Arijit
Keen Chen
Ranch Hand

Joined: Nov 12, 2002
Posts: 47
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?


SCJP 1.4 100% @ Peking, China <br />~~~~~~~~~~~~~~~~~~~~~<br />但使龙城飞将在, 不教胡马度阴山!
ramki srini
Greenhorn

Joined: Aug 27, 2002
Posts: 26
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

Joined: Sep 23, 2002
Posts: 109
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


--Shweta<br />SCJP 1.4 <br />SCWCD
Arijit Ghosh
Ranch Hand

Joined: Feb 01, 2002
Posts: 174
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

Joined: Oct 23, 2002
Posts: 29
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


SCJP
shweta mathur
Ranch Hand

Joined: Sep 23, 2002
Posts: 109
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Variable with Same Name