original code
class Initialization
{
private static String msg(String msg)
{
System.out.println(msg); return msg;
}
--------------(1)
public Initialization()
{//2
m=msg("1") ; //5
}
{
//3
m=msg("2") ;
}
String m=msg("3"); //4
public static void main(String args[])
{
Object obj = new Initialization(); //1
}
}
Here first look at the order in which code is executed.
In main method, at the time of creating object obj, its constructor is called.Look at the numbers provided by me, after that control goes to //2 i.e constructors first line. after that control moves to initializer block at //3. after that a string var at //4. and then return back to constructor.
We havent declared var m, before executing //3. m is declared at //4.
Here at //3, the var m is undeclared, hence it is giving compile time error of illegal forward reference.
if we change the code at //3 like
String m=msg("2") ;
then there wont be any error.
I hope u get it