Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do declarations following conditions of control structures need to be in a block?

Tags:

java

syntax

When trying to compile the following

public class Test {
    public void method(String foo) {
        // This compiles if the curly braces are uncommented
        if(foo instanceof Object) // {
            Object bar = (Object) foo;
        // }
    }
}

I get the following errors

javac -Xlint:all Test.java
Test.java:5: error: not a statement
            Object bar = foo;
            ^
Test.java:5: error: ';' expected
            Object bar = foo;
                  ^
2 errors

Why does Object bar = (Object) foo; need to be in a block for the code to compile?

like image 566
N.N. Avatar asked Nov 30 '12 14:11

N.N.


People also ask

Why are control structures required in a program?

Control Structures can be considered as the building blocks of computer programs. They are commands that enable a program to “take decisions”, following one path or another. A program is usually not limited to a linear sequence of instructions since during its process it may bifurcate, repeat code or bypass sections.

What control structures are used to repeat a block of statements?

Repetition statements are called loops, and are used to repeat the same code mulitple times in succession.

What is block control statement?

A program block is a group of statements that have the following two characteristics: They have a single entry point and a single exit point. A loop has a program block at its heart. A loop is used to repeatedly perform an operation or a block of code through the use of a conditional expression.

Is declaration structure a control structure?

the declaration is a parameter or type parameter, and the program element sequentially occurs in the body of the parameterized declaration, or. the program element is a control structure variable or iteration variable of a control structure that sequentially occurs in the namespace of the declaration.


2 Answers

Why does Object bar = (Object) foo; need to be in a block for the code to compile?

Because it's pointless to declare a variable when that's the only statement in the block. The declaration is meaningless, as you won't be able to refer to the variable in any subsequent code. (The scope of the variable would be just the declaration.)

Basically, the compiler is stopping you from doing something pointless.

In terms of the specification, this is the production you're trying to use (section 14.9 of the JLS):

IfThenStatement:
    if ( Expression ) Statement    

Now the Statement production is defined by section 14.5.

Statement:
    StatementWithoutTrailingSubstatement
    LabeledStatement
    IfThenStatement
    IfThenElseStatement
    WhileStatement
    ForStatement

StatementWithoutTrailingSubstatement:
    Block
    EmptyStatement
    ExpressionStatement
    AssertStatement
    SwitchStatement
    DoStatement
    BreakStatement
    ContinueStatement
    ReturnStatement
    SynchronizedStatement
    ThrowStatement
    TryStatement

StatementNoShortIf:
    StatementWithoutTrailingSubstatement
    LabeledStatementNoShortIf
    IfThenElseStatementNoShortIf
    WhileStatementNoShortIf
    ForStatementNoShortIf

Note there's no LocalVariableDeclarationStatement there. That only occurs in the BlockStatement production, defined in section 14.4 of the JLS.

like image 134
Jon Skeet Avatar answered Sep 22 '22 10:09

Jon Skeet


You cannot declare local scope variables in order to directly use it in parent scopes.

When you don't place curly braces, it leads to a single interpreted statement for the condition.

Thus, if this statement is an assignment, clearly this assignment wouldn't be able to be used afterwards since only parent scope (outside curly braces) is the unique way to manipulate it.

Therefore, compiler complains about this situation.

In order to avoid that and without putting curly braces, you have to do as follows:

public void method(String foo) {
        Object bar;
        if(foo instanceof Object)   //redundant here by the way...
          bar = (Object) foo;
          //...
}
like image 45
Mik378 Avatar answered Sep 18 '22 10:09

Mik378