• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I access a variable outside a loop when I set in it inside the loop?  RSS feed

 
Andrew Sepulveda
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this for loop, with the variables and .
What I want to know is how I could retrieve the variables outside of the loop

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have to declare i and j outside the loop:



HOWEVER, for this code, there's no point. i and j don't have any meaning outside the loop, and you know what their final values are anyway.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
umm...aren't you exiting this loop (and the method) the very first time you hit line 6, which is most likely going to be when x = 0 and y = 0?

Once you exit the method, i and j are out of scope, regardless of declaring them inside or outside the loop.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!