File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes local varaible doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "local varaible doubt" Watch "local varaible doubt" New topic

local varaible doubt

Ranch Hand

Joined: Apr 04, 2007
Posts: 188
for(int i=0;i<4;i++)
for(int j=0;j<5;j++){
for(int i=0;i<4;i++)
for(int j=0;j<5;j++){
System.out.print(twod[i][j] + " ");

1> It is said that in java we cant declare local variable with same name in same or enclosing scope.
2> But the above code works fine even though INT i and INT j are declared twice?
marc weber

Joined: Aug 31, 2004
Posts: 11343

With a little reformatting for clarity, isn't this what you have?

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

they are not in the same scope. each version of i and j fall out of scope when the for-loop where it is defined ends.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: local varaible doubt
jQuery in Action, 3rd edition