In this code during the execution of the program first the compiler declares and intializes the variable and then after that the instance block executes.
In the above code the variable i is not even declared whether it is an integer ,float or what it is whereas on the first code we are declaring it.Hence it will give your a compile time error of identifier expected.
Joined: Sep 06, 2009
then why is there an error in this?
i = 10;
int i = 5;
isnt i already declared when it reaches system.out?
This System.out gives you an error because it is not the correct forward referencing to correct this you have to use this.i so it specifically points to the reference variable and gives the output.
Well, in the case of forward referencing, we're not trying to read from i, we're assigning a value to it. Therefore, there's no "dangerous" operation occurring here and the compiler allows it. For more information check this link of java ranch I hope it will make it more clear...