There are two pieces of code
The first:
if(5 > 3) { int a = 5; } And the second:
if(5 > 3) int a = 5; The second gives an error. Java 1.7, Win7 x64. Explain please why the second gives an error?
There are two pieces of code
The first:
if(5 > 3) { int a = 5; } And the second:
if(5 > 3) int a = 5; The second gives an error. Java 1.7, Win7 x64. Explain please why the second gives an error?
To declare a variable, you need scope. In the second case it is not.
The compiler cannot place the variable a in the external scope, because if the if -thiste fails, the variable will not be initialized.
Source: https://ru.stackoverflow.com/questions/267921/
All Articles
ifbranch, what should be with the variable? Should it be available in the scope where theif, even if the execution went byelse? How should it be initialized in this case? Because Java developers did not find satisfactory answers to these questions, they did not allow this. - VladD 2:55