Jesper de Jong wrote: It's all quite straightforward. Is my explanation clear enough, or do you have any more specific questions about this?
Sachin Tripathi wrote:
I know I am horribly wrong with my assumption as I know main method executes way after class loading time
But how class loading even starts ?
I hope it isn't out of date now.
This is what I wrote:You can read all about it in the Java® Language Specification=JLS, though that chapter is one of the more difficult parts of the JLS to read. §12.4.2 is probably the most useful part at present. It says that the class is loaded immediately before its static methods are called, in this case just before the main method starts. You notice that the superclass is loaded and its initialisers run first, then the subclass initialisers, then only can the main method run.
The objects are not instantiated until after the main method starts, and in the order java.lang.Object→superclass→subclass.
How could you call the main method before the class has been loaded? If the class has not been loaded and a Class<?> object created, there is no way to find the main method.
Olga Matveeva wrote: also think about going deeper and asking "why is it exactly so", but not quite sure that my question would have a point... Because in general it is how that language is created, right?
Sachin Tripathi wrote:How do the class gets loaded and initialized ?I used to think when any particular class ,gets mentioned in main method then it gets loaded
So according to me
Should have been the first output line
Where on earth did you find that? That looks incorrect. The correct order is in the JLS links you have been given already. Those are instructions about how every Java® implementation must behave.
Sachin Tripathi wrote: . . .
But if the class (X) specified don't have a direct superclass(avoid Object class) then it load itself first and if class (X) is having "Has a" relationship with any class(Y)
. . .