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 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 commented for loop means the same as the above for loop (the same thing is also applied to a while loop) doesn't work at all causing a compile-time error that indicates that "not a statement" means that in this particular situation, the pair of braces are mandatory even though the for loop contains only one statement! Why?