This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Doubts about order of initialization

 
Ranch Hand
Posts: 116
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,

I have some doubts about the order of initialization of the elements of a class.
I found this topic: https://coderanch.com/t/678739/certification/Order-Initialization
I understand the output of that code, but I still have some doubts, and I hope you guys can help me. I thought about posting in that topic, but I decided to create a new one. If I did a bad thing, please tell me.

Well, let's go:

The book gives us the following code:

The output is 2 4 6 8 5

So, what I understand about it is: although the class aren't being referenced, it has a main method that is going to load it, is that right? So, with the class being initialized, the static initializers and static variables are loaded in the order that they appear.
First the add(2) is called, then add(4), and then the last static initializer, which creates a new object by calling new AnotherTest();
When this instantiation occurs, it's time for the instance variable and initializers to be initialized. So, first the statement add(6) is processed, then add(8), and then the constructor (last element to be initialized).
Ok, I get it. But I decided to move the static initializer with the new AnotherTest() statement, and now I have this code:

Now the output is 2 6 8 5 4. I'm trying to understand it.
First, the add(2) is initialized (first static initializer). Then, we have the static initializer with the object instantiation. Looking back to the output, the next two values after 2 are 6 and 8, and both of them are initialized inside an instance initializer. Then, the constructor is initialized, outputing 5. And the last thing now is the static initializer with add(4); statement.

Taking the following code separately:

It means that it will initialize all the instance elements and constructor in order to finish this static initializer, so the other static elements can be initialized as well. Is that right?

Could anyone help me with this one?

Thanks!
 
Ranch Hand
Posts: 47
3
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

João Victor Gomes wrote:
Taking the following code separately:

It means that it will initialize all the instance elements and constructor in order to finish this static initializer, so the other static elements can be initialized as well. Is that right?

Could anyone help me with this one?

Thanks!



Hi Victor!;)

I think it's exactly as you stated. The static Initializers
run in the coded Order. So when running



The Object gets created and Its instance Initializers are run first,
and then the Constructor. After that, the static Block

( )

has finished. Then the next static Initializer runs.

You play a littel bit by commenting out parts of the code/the
initializers/Contructors and see what happens;)

Kind regards
Florian
 
João Victor Gomes
Ranch Hand
Posts: 116
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Florian, thanks for the answer!

I'm getting the idea. I did some tests, changed some code, and it's more clear now.

I think my confusion started because of the static initializer that instantiates a new object.
What I understand is: when using the classname to refer to a static element, the static initializers and static variables are all loaded in the order that they appear. But if we instantiate a new object, both static and instance elements are loaded in the predefined order (static first, then instance, then constructor).
That's what I'm struggling with. I changed the code to the following:

When executing the class Test, the output is 2 6 8 5 4 6 8 5
So, I'm trying to answer the question inside the comment. When the code reaches the static block with the instantiation inside, this instantiation will just load the instance elements and constructor, or it will also continue to initialize the next static initializer?
I know that the instance initializers will be applied for every instance we create. But if using new AnotherTest() is going to load static elements, instance elements and then the constructor, so which instance is responsible for loading the following block:

Because since we are creating a new object, the class will be loaded, so the static initializers will be loaded as well. Am I right?

What am I missing?

Thanks
 
author
Posts: 23835
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

João Victor Gomes wrote:
So, I'm trying to answer the question inside the comment. When the code reaches the static block with the instantiation inside, this instantiation will just load the instance elements and constructor, or it will also continue to initialize the next static initializer?
I know that the instance initializers will be applied for every instance we create. But if using new AnotherTest() is going to load static elements, instance elements and then the constructor, so which instance is responsible for loading the following block:



Static initializers and static fields initializations are done, and in a particular specified order, when the class is loaded. The class is loaded when it is needed.  Period. In this example, you just happened to have instances being created in a static block, but that doesn't change the static initialization. The next static block will run when the previous one finishes.... this is true regardless of whether instances are created in the static block (or not).

João Victor Gomes wrote:
Because since we are creating a new object, the class will be loaded, so the static initializers will be loaded as well. Am I right?



No. The class has already been loaded, and it is still in the middle of static initialization. Creating a class, when the class is already loaded, does not load the class again.

Henry
 
João Victor Gomes
Ranch Hand
Posts: 116
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:

João Victor Gomes wrote:
So, I'm trying to answer the question inside the comment. When the code reaches the static block with the instantiation inside, this instantiation will just load the instance elements and constructor, or it will also continue to initialize the next static initializer?
I know that the instance initializers will be applied for every instance we create. But if using new AnotherTest() is going to load static elements, instance elements and then the constructor, so which instance is responsible for loading the following block:



Static initializers and static fields initializations are done, and in a particular specified order, when the class is loaded. The class is loaded when it is needed.  Period. In this example, you just happened to have instances being created in a static block, but that doesn't change the static initialization. The next static block will run when the previous one finishes.... this is true regardless of whether instances are created in the static block (or not).

João Victor Gomes wrote:
Because since we are creating a new object, the class will be loaded, so the static initializers will be loaded as well. Am I right?



No. The class has already been loaded, and it is still in the middle of static initialization. Creating a class, when the class is already loaded, does not load the class again.

Henry



I got it.
In the example that I was trying to understand, the object instantiation is done inside a static initializer. So, I just have to think that this instantiation is part of the static block, and the next static initializer will be loaded following the right order.

Thanks again Henry.
 
Henry Wong
author
Posts: 23835
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

João Victor Gomes wrote:
In the example that I was trying to understand, the object instantiation is done inside a static initializer. So, I just have to think that this instantiation is part of the static block, and the next static initializer will be loaded following the right order.



Be careful with the terms. By the time the static initializer (even the first one) runs, the class has been completed loaded*. Additionally, all the static resources, such as static variables have been allocated.  It is just that they haven't been initialized yet, so, they have the default values.  Static initializers are part of the initialization process -- after the loading and resource allocation for the class have been completed.

Henry

Note: Of course, in this response, the term "loaded" means the loading of the class bytecodes from file. It is not referring to the whole class loading process.
 
João Victor Gomes
Ranch Hand
Posts: 116
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:

João Victor Gomes wrote:
In the example that I was trying to understand, the object instantiation is done inside a static initializer. So, I just have to think that this instantiation is part of the static block, and the next static initializer will be loaded following the right order.



Be careful with the terms. By the time the static initializer (even the first one) runs, the class has been completed loaded*. Additionally, all the static resources, such as static variables have been allocated.  It is just that they haven't been initialized yet, so, they have the default values.  Static initializers are part of the initialization process -- after the loading and resource allocation for the class have been completed.

Henry

Note: Of course, in this response, the term "loaded" means the loading of the class bytecodes from file. It is not referring to the whole class loading process.



Yes, you right. I meant to say the right thing, but I used the wrong words.

Thanks for the reminder.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!