2

the following code gives an error, because the variable m was defined twice.

class one {
   public static void main(String args[]) {
     int m=10;
     int m=10;
   }
 }

but when the declaration is done inside a loop, it is OK, even though m is still being defined twice.

 class one {
   public static void main(String args[]) {
     for(int i=1;i<=2;i++) {
       int m=10;
     }
   }
 }

and the compiler does not give back an error message.
can you explain the differences between the two, and how come sometimes i can declare the same variable twice inside the same method, and sometimes not?

3
  • 6
    Well, read the error message. It should be self-explanatory as to why the first usage is invalid. Then, if there is a remaining question, please post the exact error message.
    – user166390
    Sep 29, 2012 at 17:38
  • @adchilds That terminology seems misleading .. there is no "overwriting" or "redeclaring" as far as I know. Although demonstrating that a new variable binding is introduced can be observed through closures mocked with final variables and anonymous inner classes. (Although I could be wrong on this point, if final is viewed as doing no more than a copy-on-instantiate of the anonymous class.)
    – user166390
    Sep 29, 2012 at 17:43
  • Why was this downvoted? I found this to be a pretty valid question! Sep 29, 2012 at 18:25

6 Answers 6

2

For the first case m is referenced till the end of the main method so you can't have two variable of the same name in the same scope.

Whereas in the second case, for every time the loop executes, m for the last iteration is no longer referenced and hence you are able to redeclare and reinitialize it.

0

You cannot declare a variable with the same name more than once in a block of code.

In first case, you are declaring the same variable in a block of code i.e. main. In second case, after the first iteration of for loop, variable m is destroyed and recreated over the second iteration

0

It's creating an error in the first one because you are declaring the varible twice.

1
  • I think the OP already knows that you can't declare a variable twice. What he wants to known is why it's ok to declare a variable inside a loop if each iteration declares the variable again. Don't be trigger happy. Understand the question :) Sep 29, 2012 at 18:23
0

you get the error is because you defined the same variable twice in the same block (scope). when you run inside a loop, you "open" a new scope for every iteration of the loop, so you can define a variable that is visible only within this scope (won't be accible outside the loop though). for instance, if you had written something like that:

class one {    
    public static void main(String args[]) {     
        {
            int m=10;
        }
        {
            int m=10;
        }
    }
}

it would have been compiled just fine, because the variables of the same name, does not share the same scope.

0

- In the First code you have declared m twice in the same scope, and it continues till the main() method ends.

- Where as within the loop everytime a primitive int variable m is created with a value, so its obviously not a problem.

0

In simple words, you are not declaring this variable twice in second example. As variable lifespan ends on the closing } of the block in which it was declared, by the time you declare m in second iteration, first one is "dead". This is why you can do it in a loop.

In first example you declare two variables with the same name in the same code block, which are supposed to "live" simultaneously. This is forbidden as you can't tell which variable you refer too by writing m.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.