The operator to create an object in Java does not allow the use of a single-line loop. What for?

The following program does not matter. It simply counts the number of objects created using the for loop using a static field inside the Counter class, as shown below.

package temp; final class Counter { private static int cnt; public Counter() { cnt++; } public static int show() { return(cnt); } } final public class Main { public static void main(String[] args) { for (int i=0;i<50;i++) { Counter counter=new Counter(); } /*for (int i=0;i<50;i++) Counter counter=new Counter();*/ System.out.print("\nNumber of objects constructed:->"+Counter.show()+"\n\n"); } } 



The only question here is that the requested loop means the same as above for the loop (the same applies to the while loop) does not work at all, causing a compile-time error that indicates that β€œno statement” means that in In this particular situation, a pair of curly braces is mandatory, although the for loop contains only one statement! Why?

+19
java
Nov 16 2018-11-11T00:
source share
4 answers

To understand why this is happening, you should look at the Java Blocks and Statement syntax in the language specification.

A ForStatement is defined as:

 ForStatement: for ( ForInitopt ; Expressionopt ; ForUpdateopt ) Statement 

An operator is defined as:

 Statement: StatementWithoutTrailingSubstatement LabeledStatement IfThenStatement IfThenElseStatement WhileStatement ForStatement StatementWithoutTrailingSubstatement: Block EmptyStatement ExpressionStatement SwitchStatement DoStatement BreakStatement ContinueStatement ReturnStatement SynchronizedStatement ThrowStatement TryStatement 

Then, looking at the block:

 Block: { BlockStatementsopt } BlockStatements: BlockStatement BlockStatements BlockStatement BlockStatement: LocalVariableDeclarationStatement ClassDeclaration Statement 

You will notice that in this specification, LocalVariableDeclarationStatement is not valid if it is not in a block. But, since ForStatement requires an operator to follow it, parentheses MUST exist there to make the expression valid. Thus, any declaration of a local variable would be invalid in a loop without parentheses.

+27
Nov 16 2018-11-11T00:
source share

Because you are creating a scope variable. Java tells you that it does nothing, because all it does is allocate memory, and as soon as the loop goes through again, you lose it and make a new one. Essentially, the whole loop is NOP, so it tells you that it comes down to a do do statement.

If the loop is NOP, I mean that the declaration in the loop is NOP.

The reason the curly brackets version works is because the compiler does not test usage within the scope because there are curly braces. This is likely due to the parsing tree generated from a single line operator, versus the parsing tree created with a full loop.

+6
Nov 16 2018-11-11T00:
source share

@JasCav is right, however you can get it to compile:

 for (int i=0;i<50;i++) new Counter(); 
+4
Nov 16 2018-11-11T00:
source share

You can also make it work without braces this way:

 Counter counter; for (int i = 0; i < 50; i++) counter = new Counter(); 

Not that there is any reason why you would ever want to do this. Leaving the brackets off is really a bad idea, because adding an operator actually changes the control flow.

+4
Nov 16 '11 at 2:00
source share



All Articles