Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

local variable

 
Naresh Gunda
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi
variables defined in a for-loop are local to the forloop. Code outside the for loop can't access the local variables defined in a for-loop.

But the above program is giving compilation error. Can any one of u tell me the reason?
 
Cristina Vinuela
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

But the loop can access the method variables, that's why the compiler finds two i variables IN the loop, the local one and the method one.

Hope it helps

Cris
 
Rick Allen
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I arranged the code given and hand no compiler error. Hope this helps.

class Test{

int i=5;


public static void main(String args[]) {

for(int i=1;i<=5;i++,System.out.println("welcome"));


}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic