20

While working on something, I stumbled upon this piece of code:

if(true) {
    String message = "Assignment possible";
}

if(true)
    String message = "Time to leave earth";  // error!

Why is there a compilation error in the second case when it is possible to write 'if' conditions in java without the braces?

The error message doesn't seem to give a proper info:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Syntax error, insert ":: IdentifierOrNew" to complete ReferenceExpression
Syntax error, insert "AssignmentOperator Expression" to complete Assignment
Syntax error, insert ";" to complete Statement
String cannot be resolved to a variable
message cannot be resolved to a variable

Am I missing something obvious here? Can someone explain this?

Eran
  • 387,369
  • 54
  • 702
  • 768
Vinu Dominic
  • 1,040
  • 1
  • 16
  • 25
  • Is this in the same file? I think conditional definition doesn't work, and the previous statement already defined a String called "message". The second statement tries to redefine it, and this gives this strange error message. – 0xCAFEBABE Dec 23 '15 at 09:08
  • Where would you use that string `message` ? if there is no block you can't use it after .. It makes no sense to write this – Hacketo Dec 23 '15 at 09:09
  • 1
    To be obtuse, it is not allowed because the [language spec](http://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.5) doesn't allow it; see the linked duplicate for more in depth explanations. – Mark Rotteveel Dec 23 '15 at 09:22

2 Answers2

14

You can't declare a variable inside an if statement unless you surround the declaration with curly braces, since the declaration must have a scope.

This would work, since the variable is declared outside the if statement :

String message;
if(true)
    message = "Assignment possible";
System.out.println(message)

This doesn't pass compilation, since the variable would be declared in the same scope as the surrounding code, but it would only be declared if the condition is true, so System.out.println(message) will not have a message variable to print in all cases :

if(some condition)
    String message = "Time to leave earth";
System.out.println(message);

The fact that your condition is always true doesn't make a difference, since the compiler must support all outcomes of any if condition.

Now, when you surround the declaration with curly braces, you restrict your declared variable to a new scope :

if(some condition) {
    String message = "Time to leave earth";
    System.out.println(message); // this will pass compilation since message
                                 // is guaranteed to be found in this scope
}
System.out.println(message); // this won't pass compilation, since message is not 
                             // found in this scope
Eran
  • 387,369
  • 54
  • 702
  • 768
0

The controlled-statement of an if statement is either a single statement or a block. A single statement cannot be a declaration-statement.

user207421
  • 305,947
  • 44
  • 307
  • 483