Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

local varaible doubt

 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for(int i=0;i<4;i++)
for(int j=0;j<5;j++){
twod[i][j]=k;
k++;
}
for(int i=0;i<4;i++)
for(int j=0;j<5;j++){
System.out.print(twod[i][j] + " ");

}
/*******doubt******************/
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
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With a little reformatting for clarity, isn't this what you have?
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic