Dear all,
This
thread is really interesting. Thanks
Mr. Shah Chunky. Everybody tried to solve the problem, but I think none of them had a real satisfactory answer.
Even I can't solve the problems. But I can guess and want share it with others and check whether I am right.
The first problem is the compiler shouts when I write the following :
while(...)
int i = ...;
The same problem arises when I replace
while with
if. The compiler smiles if I include the statement with braces like the following :
while(...)
{
int i = ...;
}
The question is why or whether the compiler should behave like this. According to me the compiler behaves like this, because it does not want to
confuse 
the developers. But where is the question of confusion? Look at the following :
int i = 3 ;
if(i == 3)
int j = 4 ;
We know that here
j j can't be accessed outside the
if statement. But the code seems to be that
j is defined in the outer scope and it gets defined only if the value of
i is equal to 3. There is a confusion because later we may want to refer
j in the outer scope and it will fail failing us to quickly debug that where the problem is. (You may shout "Oh boy, why are not you getting compiled? Be steady and look there that
i equals 3 and thus
j is defined and has got a value of 4"

.)
Actually such a loop or
if construct that contains merely a statement which does nothing but defines a variable has NO PRACTICAL USAGE (of course, according to my limited knowledge). If we still want to write such a statement (just for fun, isn't so!) we have to write it more clearly and more specifically so that the statement can tell "Hey, now you see I am in the inner room, you can't get me outside this room".
If it is the real fact, I would thank
JAVA.
But i don't know what about the second problem. I am using Java 2 and get the problem. Since many of the responders faced no problem with Java 3, I can conclude that the complier creators have realised that "there is no harm with determining the value of the final variable (because it gurantees to be never changed) and then to decide whether to allow the developer to compile the code.
But I (we) still wait for some more better and confident answer. Hey,
William Brogden,
Cindy Glass,
Paul Anil,
Junilu Lacar and all, can you here me (us)? Where are you? Please help me (us).
Prosenjit Banerjee.
I LOVE JAVARANCH.COM