This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

local varaible doubt  RSS feed

 
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
Posts: 12542
48
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
Boost this thread!