• Post Reply Bookmark Topic Watch Topic
  • New Topic

Order of execution in sample code  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am completely lost in the logic of execution of that sample code. Could anyone explain what´s going on there, how order of execution is like that?







The output is

super static block
static block3
in main
super constructor
constructor
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static initializer code, such as what you have in the static blocks, is executed when the class is loaded.

The superclass StaticSuper is loaded and initialized first, so the first line that you see is from the static block in class StaticSuper.

Then the subclass StaticTest is loaded and initialized, so the second line is from the static block in class StaticTest.

Note that no instances of class StaticTest or StaticSuper has been loaded until this point. Only the classes have been loaded and initialized by the JVM.

Then the main() method is executed in class StaticTest. This method creates an instance of class StaticTest. When this is done, the constructor of the superclass StaticSuper is called first, and then the constructor of the subclass StaticTest.

It's all quite straightforward. Is my explanation clear enough, or do you have any more specific questions about this?
 
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your explanation is quite clear
I know that that extending class has to call its super class constructor before calling its own
But your explanation creates one silly doubt in my mind

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
in main
Should have been the first output line


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 ?
 
Olga Matveeva
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote: It's all quite straightforward. Is my explanation clear enough, or do you have any more specific questions about this?


It is quite clear and you explained that by definitions of Java machinery. Thanks a lot!

I could 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?
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?


When you specify the class (with the main) as the parameter to the "java" command. It will find and load the class for use. And as the class is being used, it will determine what else needs to be loaded (to be used).


And BTW, if you haven't figured it out yet. The order that the classes get loaded, isn't exactly the order that the static initializer runs. I never quite figured it out myself, so if anyone wants to try to explain, feel free...

Henry
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah anyone do help us
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote the following earlier, about 3 hours ago, and something went wrong and it wasn't posted.
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.
I hope it isn't out of date now.
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class initialization takes the following order:

1) Initialize all static fields that are constant variables.

2) Initialize all superclass/super interfaces.

3) Execute all initializers in textual order.

See the Java Language Specification, section 12.4.2.

docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.4.2
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I
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?

Actually as you can't consider a child is born before their parent even exist so whenever you will call the constructor of Child class
Constructor of parent is automatically called
To prepare the parent
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Child and parent are not at all good analogies for inheritance in object‑orientation.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya but it makes thing easy to understand and easy to explain (don't they???)
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you look at the structure of an object, you find the analogy breaks down. For a start, part of each subclass object is a superclass object.
The subclass object is often larger than the superclass object, which is not the case with child and parent.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
in main
Should have been the first output line

But in this case, the main method is in the StaticTest class itself. So, that class obviously has to be loaded first before the main method can be executed.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we specify class name(X) with "java"
Then that class (X) will load all its super class then itself
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)
Then that class (Y) will get loaded depending upon its position (if it is(Y) mentioned in instance field then it will be loaded after creating object of (X) and if it is (Y) mentioned in main method of (X) then it(Y) will be loaded when main executes)
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
. . .
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
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will you specify the exact portion
Where my train went off the track
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!