Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

the running process of the following code

 
stan freman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And


The output is : 1,4,2,3
I am not sure why. My understanding is the program starts from the main method, so only 3 should be printed. Apparently I am wrong, can anyone explain the correct order of running this code?

Thanks
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stan,

First of all, a warm welcome to CodeRanch!

stan freman wrote:I am not sure why. My understanding is the program starts from the main method, so only 3 should be printed. Apparently I am wrong, can anyone explain the correct order of running this code?

You are correct if you state the program starts from the main method.

But before the main method can be executed, the class (and its superclasses) containing this method must be loaded first. When a class is loaded, all static initializer blocks (none in your example) and static (class) variables are initialized in the order they occur in the class file. And another thing to note: class B has an instance variable a of type A. So with every creation of a new B instance, an A instance will be created as well (and 2 will be printed).

So step by step it looks like this:
1/ load class B --> static (class) variables are initialized
1a/ class variable s1 is initialized, refers to an A instance --> 1 is printed
1b/ s2 is initialized, refers to an A instance --> 4 is printed

2/ main is executed
2a/ local variable b is initialized, refers to a B instance
2a1/ when the new B instance is created, the instance member a (defined in class B) is initialized as well, refers to an A instance --> 2 is printed
2b/ local variable a is initialized, refers to an A instance --> 3 is printed

If you add a few more println-statements and some static initializer blocks, you (probably) get a better understanding of what's going on. Have a look at this code snippet and try (with the explanation I provided) to understand the output:Output:
init1
1
init2
4
init3
begin main
2
3
end main


If you still have doubts/questions, don't hesitate to post them in another reply.

Hope it helps!
Kind regards,
Roel
 
stan freman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very very nice explanation. Thank you so much sir. Completely answered my question
 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi stan freman,

Thanks for your this post, I always had a doubt in my mind whether a instance variable initialization is done first or the constructor finishes first, I modified your this code a little and got my answer, here is the code if any one wants to know.





Thanks,
Prathima
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathima gaitonde wrote:I always had a doubt in my mind whether a instance variable initialization is done first or the constructor finishes first

And what about initializer blocks, both instance and static?
 
Suyash Gulati
Ranch Hand
Posts: 37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you both to learn this order :
static -> instance -> constructor

static and instance includes both variables and blocks.
And which static/instance variable will compile first depends on the order they occur.
This is how generally you should solve questions regarding this.

But for above question Roel has given the best possible explaination.
 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Suyash Gulati, Thanks for the suggestion,

Hi Roel, there is no need for me to have the doubt about static and non static initializer block, especially getting support from people like you and others on this site.

1> static initializer block gets executed even before the class gets initialized, i.e its objects getting created, and it is done only once when the class is loaded
2> When will the class get loaded is explained several times on this site, cant remember the exact thread.
3> Instance initializer block get executed whenever the instance of the class is created.

In summary order goes like this:



1>In the above program on exicution of B.java file, static blocks and variables of class A is executed in there order of existance, then static blocks and variables of B,
2> Why is This? Is, main method which is static is called by JVM i.e when the class B is loaded.
3> In the above code no instance initializer block or variables are executed, nevertheless these instance variables(if any) are created, with there default values
4> If we add new B(); in main method now
a> default constructor of B() is called,
b> default constructor of A() is called,
c> All the instance initializer blocks of A() get executed
d> Constructor of A() finishes
e> All the instance variables get there explicitly initialized values(if any)
and same step from c to e for class B() will be executed.

Please correct me if I am still wrong( after 4 months of studies.. )

With great respect,
Prathima
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathima gaitonde wrote:Hi Roel, there is no need for me to have the doubt about static and non static initializer block, especially getting support from people like you and others on this site.

Glad to hear! I need to make a few small remarks (some just verbiage). Here we go!

Prathima gaitonde wrote:1> static initializer block gets executed even before the class gets initialized, i.e its objects getting created, and it is done only once when the class is loaded

Keep it short and simple: a static initializer block is executed once, when the class is loaded (not initialized). A class can have many static initialization blocks, and they can appear anywhere in the class body. The static initialization blocks are executed in the order that they appear in the source code (from top to bottom).

Prathima gaitonde wrote:2> When will the class get loaded is explained several times on this site, cant remember the exact thread.

More info about when a class is loaded and how you can display the loaded classes can be found in this thread.

Prathima gaitonde wrote:4> If we add new B(); in main method now
a> default constructor of B() is called,
b> default constructor of A() is called,
c> All the instance initializer blocks of A() get executed
d> Constructor of A() finishes
e> All the instance variables get there explicitly initialized values(if any)
and same step from c to e for class B() will be executed.

Almost correct! A few remarks:
1/ you forget 1 (important) thing: the default constructor of A will first call the default constructor of Object
2/ also the execution order of constructor and instance initializer blocks and when instance variables get their (default) values is slightly off, as illustrated in this code snippet:
Output:
static init abc
static init def
init1 abc
init2 abc
ctr abc
init1 def 0
init2 def 0
ctr def 5


And a post of mine would not be complete if it didn't have a pop quiz question So here it is: what will happen if instance variable var is made final? Does it still compile? And if it does, what's the output?

Hope it helps!
Kind regards,
Roel
 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



And a post of mine would not be complete if it didn't have a pop quiz question So here it is: what will happen if instance variable var is made final? Does it still compile? And if it does, what's the output?


Hi Roel,

I am not perfect, and still trying to climb the ladder, not even on the first step, so sorry for the mistakes made in my last posts, Again thanks to the support given by this site, happy to answer your question,

1>A variable declared final static and initialized there and then is called compile time constants,
2>Such variables are (according to my understanding) constants, whose value cant be changed later, and where ever they are used compiler knows their value for sure.
3>A variable which is final and static and not initialized is not a compile time constant, whose initialization can be postponed until its static block finishes.
4> Variable declared as only final not static are instance variable which are constants, whose assignment can be postponed till constructor finishes.

If we try to use such variables (declared in point 3 & 4) before there initialization, compile time error occurs, hence answer to your question is if var is final in above code, instance initializer blocks cant use it before var getting initialized, but this below code is fine.

and the value of var will remain 10 in second instance initializer block as well, as it is a final instance variable.

I hope, have answered this without any mistakes,

With great respect,
Prathima
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathima gaitonde wrote:I am not perfect, and still trying to climb the ladder, not even on the first step, so sorry for the mistakes made in my last posts

No worries! We all (even me) are here to learn. And you learn best from your mistakes!

Prathima gaitonde wrote:I hope, have answered this without any mistakes,

Almost perfect! Would award you with 9.75 out of 10

Prathima gaitonde wrote:4> Variable declared as only final not static are instance variable which are constants, whose assignment can be postponed till constructor finishes.

Only 1 (small) remark: as your code example shows a final instance variable can also be initialized in an instance initializer block. So you'll have 3 ways to initialize a final instance variable: assign a value instantly OR in a constructor OR in an initializer block. A nice overview of initializing (final) variables (static, instance and local) and the consequences can be found in this great post. (Disclaimer: it's one of my own posts )

Hope it helps!
Kind regards,
Roel
 
Daniel Andres
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:And a post of mine would not be complete if it didn't have a pop quiz question So here it is: what will happen if instance variable var is made final? Does it still compile? And if it does, what's the output?

Hope it helps!
Kind regards,
Roel


I think it still runs because while it is not initialized on line 9 it is on line 11. Is this correct?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Andres wrote:I think it still runs because while it is not initialized on line 9 it is on line 11. Is this correct?

No, you are unfortunately wrong!

And I think you'll be able to give the correct answer on a second attempt. I'll even give you a tip This is the output of the original code snippet:
static init abc
static init def
init1 abc
init2 abc
ctr abc
init1 def 0
init2 def 0
ctr def 5


Now focus on the 3 last lines (the ones in bold). And then have another go what will happen if instance variable var is made final?

Hope it helps!
Kind regards,
Roel
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the order spelled out, borrowed from an Enthuware answer (I think).

1. Static blocks of the base class (only once, in the order they appear in the class).

2. Static blocks of the class.

3. Non-static blocks of the base class.

4. Constructor of the base class.

5. Non-static blocks of the class.

6. Constructor of the class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic