Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The curious case of "if" in Java [duplicate]

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?

like image 497
Vinu Dominic Avatar asked Dec 23 '15 09:12

Vinu Dominic


People also ask

How to execute code if condition is false in Java?

Use the else statement to specify a block of code to be executed if the condition is false. int time = 20; if (time < 18) { System.out.println("Good day."); } else { System.out.println("Good evening.");

What is condition after evaluation of if-statement in Java?

The condition after evaluation of if-statement will be either true or false. The if statement in Java accepts boolean values and if the value is true then it will execute the block of statements under it.

What happens when the test condition is true in Java?

When the test condition is true, then only the code within this will run. If statement in Java Programming language has a simple structure that returns true or false: if (test condition) { Statement1; Statement2; Statement3; …………. ………….

What is the use of if in Java?

Java If ... Else Java If ... Else Java supports the usual logical conditions from mathematics: You can use these conditions to perform different actions for different decisions. Use the if statement to specify a block of Java code to be executed if a condition is true. Note that if is in lowercase letters.


1 Answers

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
like image 172
Eran Avatar answered Sep 26 '22 13:09

Eran