I guess the reason could be that the compilation takes place in 2 passes in the first pass the variables are identified(forward references). after that assignment of values takes place as and when it happens
In the following program, how does compiler know that 'i' is of integer type [Dont know exactly the order in which compiler starts checking]. However, at runtime how does JVM know about the type, if the initializers and members execute from top to bottom.
[ December 16, 2004: Message edited by: Shourya Kalinga ] [ December 16, 2004: Message edited by: Shourya Kalinga ]
A compiler has read your entire program and created a tree structure and symbol table before it starts to validate the types of the variables used in expressions.
The jvm knows the types of all the variables in a class from the .class object, which is in memory before any instance of the class begins to be created. Early in the object creation process, there is a point where memory has been allocated for all the member variables and they have been initialized to their default values (0, false, or null). After that, the initializers and initializer blocks are executed in the order they appear in your program. After that, the constructors execute. [ December 16, 2004: Message edited by: Mike Gershman ]
SCJP 1.4, SCWCD in process
Joined: Sep 13, 2004
Looks like this is an interesting question and I could see lot of interesting discussions on this..