Object creating statement in Java doesn't allow to use a single-line loop. Why?

前端 未结 4 1358
遇见更好的自我
遇见更好的自我 2020-12-11 01:03

The following program has no importance of its own. It just counts the number of objects created through the use of a for loop using a static field inside the class Counter

4条回答
  •  星月不相逢
    2020-12-11 01:17

    You can also get it to work without braces this way:

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

    Not that there's any reason you'd ever want to do such a thing. Leaving off the brackets is a really bad idea because adding a statement actually makes the control flow change.

提交回复
热议问题