Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
The following program has no significance 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.

Java
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 created:->"+Counter.show()+"\n\n");
    }
}


The only question here is that the commented for loop means the same as the preceding for 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?
Posted
Comments
Sergey Alexandrovich Kryukov 21-Jan-12 17:51pm    
This question seems naive, but it helps to reveal a delicate aspect of how a compiler works.
So, I decided to vote 5 for this question, which happens quite rarely.

Thanks for interesting question. I answered in detail, please see.
--SA

1 solution

In this case, the compiler serves you quite well.

This loop code makes no sense — in both commented and uncommented variants. In each of the loop iterations you create a new Counter object and assign it to the variable locally defined in the loop, and later you loose this value in each iteration as you create a new object and a new variable in each iteration and get the old object lost. The call to the compiler without assignment could have some sense in principle, because it could be called just for side effect. It would be a bad stile, but formally valid. So the problem is having the unused local declaration and useless assignment.

Now, why the compiler detects the problem in one case and not in another case while both cases are wrong? Well, the compiler is not so intellectual. I'll try to explain its "thinking" which is pretty straightforward.

The compiler is able to detect this pathological case only without the block, which is kind of easier. The compiler can "see" that the the only statement under the loop (it is called embedded statement) is a variable declaration, which never can make any sense, no matter if is it followed by assignment or not. In case of the block {/*.../} compiler just stops to analyze the situation deeper; so the compiler logic here is "who knows what comes next?". Indeed, it you added another line in the block, and it that second line used the value of counter, it would perfectly justify the declaration. In case of embedded statement syntax, such addition is impossible in principle.

The compiler is designed to analyze the code to some reasonable depth which makes perfect sense, if you think about it. Set aside performance, technical problem and the problem of supportability, if a compiler is too "intellectual", the advanced error messages would be too complex and more confusing then helping. Look at my present answer. It is already long enough, almost a short article. Now imagine the compiler which gives out you some error messages of the size of the whole article, now and then. :-) So, the analysis of the problem code should be simpler, easier to falsify and strictly formal, directly referencing the definition of the language.

The error message is less than perfect, but it's still better then nothing. :-)

—SA
 
Share this answer
 
v7
Comments
Lion 2012 21-Jan-12 19:32pm    
Great! Helpful answer. I will surely vote your post later when my email address is confirmed. Thank a lot
Sergey Alexandrovich Kryukov 21-Jan-12 19:37pm    
I'm glad my answer works for you.
Wish you to keep your interest to the fine detail.

Good luck, call again.
--SA
Espen Harlinn 22-Jan-12 6:49am    
5'ed :)
Sergey Alexandrovich Kryukov 22-Jan-12 12:55pm    
Thank you, Espen.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900