A single-line loop with a mandatory pair of braces in Java

Solution 1:

When you declare a variable (main in this case):

Main main = new Main();

it doesn't count as a statement, even if it has side-effects. For it to be a proper statement, you should just do

new Main();

So why is

... {
    Main main = new Main();
}

allowed? { ... } is a block of code, and does count as a statement. In this case the main variable could be used after the declaration, but before the closing brace. Some compilers ignore the fact that it's indeed not used, other compilers emit warnings regarding this.

Solution 2:

It can make sense to create a one-line block with a new statement. What doesn't make sense is to save a reference to a just created object inside a one-line block, because you can't access variable main from outside the for scope.

Perhaps (just my guess) the compiler forces you to type the brackets explicitly because holding a reference doesn't make sense in that case, with the hope you become aware of the useless reference.