3
public class Test1{
    public static void main(String[] args){
        int x = 3;
        do {
            int y = 1;
            System.out.print(y++ + " ");
            x--;
        } while(x >= 0);
    }
}

In above code, local variable y is in scope for the block of do while statement. This is valid for all the iterations of the loop. So why does java not throw an error "Variable Y is already defined" for subsequent iterations after the first one, as we are redeclaring the same variable in each iteration?

6
  • 5
    Basically it's a new variable on each iteration. Commented Feb 8, 2018 at 14:10
  • "This is valid for all the iterations of the loop." - Actually, no it is not. The value of y does not last from one loop iteration to the next. (And what Jon said.) Commented Feb 8, 2018 at 14:10
  • @Jon Skeet , In that case does java implicitly copy the value of variables from older iteration to newer iteration every time? Or else in every iteration the locally defined variables will start from their default values Commented Feb 8, 2018 at 14:14
  • 1
    No, each variable is logically a "new" variable. Commented Feb 8, 2018 at 14:14
  • @MayankMadhav, even if it did something along the lines you have suggested, there would be no way to observe this, because you cannot accesss a value of the variable without declaring it first. Commented Feb 8, 2018 at 14:17

4 Answers 4

1

The variable x is in scope for all iterations of the loop, the variable y is in scope for each iteration of the loop separately.

When execution goes outside of the { } braces, y goes out of scope (and vanishes). So when while(x >= 0) is evaluated, y is not in scope. You can test that by trying to use y in the condition, you'll see an error telling you there's no y variable declared.

Sign up to request clarification or add additional context in comments.

Comments

1

So why does java not throw an error "Variable Y is already defined" for subsequent iterations after the first one, as we are redeclaring the same variable in each iteration?

The variable y die once the iteration end and a new y gets declared in next iteration.

So if you iterate 3 times, each time a new y gets declared and 3 times died.

       do {
            int y = 1; // created
            System.out.print(y++ + " ");
            x--;
           // going to die here as the scope of the block ending here
      } while(x >= 0);

Comments

1

If you unroll your loop, your code looks basically like this:

{
    int y=1; 
    System.out.print(y++ + " ");
}
{
    int y=1; 
    System.out.print(y++ + " ");
}
{
    int y=1; 
    System.out.print(y++ + " ");
}
{
    int y=1; 
    System.out.print(y++ + " ");
}

Each y has its own scope, i.e. they only exist inside their enclosing braces. So four different variables are created and 1 1 1 1 should be printed.

Comments

0

On every loop iteration, you are freshly declaring the variable y and instantiating it with value 1. Loop doesn't bother the status of the variable y down the line as the variable y is getting declared every time at the starting of the loop. So it just ignores the previous status and starts afresh as y=1 on every loop iteration.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.