• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variable scope confusion

 
geeta rai
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class JMM125
{
static int i;
public static void main(String args[]) {
for (i=1; i<3; i++) {System.out.print(i);} // 1
for (int i=1; i<3; i++) {System.out.print(i);} // 2
int i; // 3
for (i=0; i<2; i++) {System.out.print(i);} // 4
System.out.print(JMM125.i);
}}
For the above code why would the value of variable i increase to 3 and would not be 0? The answer to the above question is 1212013 and not 1212010
 
dennis zined
Ranch Hand
Posts: 330
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The last System.out.println refers to the static member variable i which was left with the value of 3 when the first for statement terminated. See code below.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic