3

This is simple but it's getting confuse me, I learnt about body scope is
first line of break point, But below 1'st if statement getting error (Not a statement) and 2'nd is fine.

I just want only, why getting error?

if (true)
    String name="Body Test";

And

if (true) {
    String name="Body Test";
}

The Exception:

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
xenteros
  • 15,586
  • 12
  • 56
  • 91
Vinod
  • 300
  • 3
  • 18

4 Answers4

4

if (true) String name="Body Test"; is invalid since the introduction of a new variable into scope cannot be conditional, as it's something that happens at compile time. Note that Java is not clever enough to realise that the if (true) will always run. The other way you've written it is absolutely fine since the { and } always enclose name.

It's somewhat akin to introducing a new variable in a particular case of a switch block, without using { and }.

4

In your example you are trying to declare a local variable name using guard clause. As in Oracle documentation Local Variable Declaration Statements is said:

Every local variable declaration statement is immediately contained by a block.

Also from The if Statement ducmentation it has the following syntax:

IfThenStatement:
    if ( Expression ) Statement

while in the Java Specification list of statements there are no declarations.

And defining a variable within the scope of a single-statement block (without braces) makes it unreachable outside the scope. As in your first case where declaration is not contained in a {} block, so compiler gives an error.

This is only about declaration, in the case of initialization it works:

String name;              // declaration
if (true)
   name = "Body Test";    // initialization (compiles okay)

as well as code below will fail by compiler:

if (true)
    String name;          // only declaration (compile error!)

At the same time Google Java Style as the best practice advice to use braces with if, else, for, do and while statements always, even when the body is empty or contains only a single statement. Braces improve the uniformity and readability of code.

DimaSan
  • 12,264
  • 11
  • 65
  • 75
  • 1
    You don't answer the question here, the question is why do we get a compilation error with the first case (which proves that they are not fully equivalent ) – Nicolas Filotto Oct 05 '16 at 14:17
  • Yes exactly @Nicolas, why can not create local variable in first case. – Vinod Oct 05 '16 at 14:19
  • "So in the first case your declaration is not contained in a block and compiler gives an error.". It *is* contained within a scope block. The problem is due to the fact that it's introduction is *conditional* on the evaluation of `if`. –  Oct 05 '16 at 14:29
  • Thanks @DimaSan, one question **String name="Body Test";** is one statement ? – Vinod Oct 05 '16 at 14:40
  • @Chump Block that is code enclosed between the `{` and `}` braces. From that point of view it's outside block. – DimaSan Oct 05 '16 at 14:40
  • @Vinod Yes it is one declaration statement that initializes the variable `name` with a value `Body test`. – DimaSan Oct 05 '16 at 14:43
  • Yes, Why not declaration statement in first case?, because if statement allow one statement without body { }. – Vinod Oct 05 '16 at 14:46
  • No, the declaration is not allowed there. If you write `if (true) String name;` you'll also get a compiler error, despite the `String name` is declaration only. – DimaSan Oct 05 '16 at 14:51
  • I agree with you, why not declare one statement without body? – – Vinod Oct 05 '16 at 14:55
  • @Vinod I edited my answer with some JSL info, please read it. Btw you have brought up a very interesting and important question, I didn't know about it before - thanks. – DimaSan Oct 05 '16 at 17:06
1

In your first example String name="Body Test"; is block statement. You can only put a statement without curly braces. Please read this Oracle's official site for more information: http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.2

0
if (true)
    String name="Body Test";

Above code block throws an error saying variable declaration not allowed here

because compiler allows only one statement after if(true) where a declaration is invalid because of the scope of that variables.

if (true) {
String name="Body Test";
}

But in the second case there is a chance of multiple lines of code so no error.

Devas
  • 1,544
  • 4
  • 23
  • 28
  • In the first case **String name="Body Test";** is one statement, So why not declaration allow? – Vinod Oct 05 '16 at 14:37
  • As I mentioned, declaring a variable is meaning less because we can't use it further in the code. You can use the following code, String name; if (true) name="Body Test"; – Devas Oct 05 '16 at 14:41
  • I agree with you, it's not matter further use. Matter is that why not declare one statement without body? – Vinod Oct 05 '16 at 14:49
  • Then someone may ask why we declare a useless variable ? Also compiler understand that this may be a bug. – Devas Oct 05 '16 at 14:58
  • are you clear about this ? these are some patterns used by java compiler to avoid some unnecessary codes and bugs. – Devas Oct 05 '16 at 16:40