Inspecting Java startups
The developers around you might know that there are some mechanism hooked when creating an object. Lets have a look at the order of these processes.
Even beginners should know about constructors. They are called if you create an object of a class, but there are some things running before. Here is an example class:
The output is:
As you can see, first of all the static initializer is called. It’s also called exactly once, when the class is loaded. It’s clear that the class has to get loaded before the main ()
inside can be executed.
The main ()
then prints a string to indicate the start of that routine and afterwards it creates the first object of the type Initializing
. This calls the instance initializer before the constructor is executed. Also the creation of the second object calls first the instance initializer and then the constructor.
That’s the workaround. At the first time a certain class is used the static initializer is executed, and each time an object of that class is created first the instance initializer is called and then the constructor.
Btw. all of these routines are able to access members that are private, but notice that the static initializer can only access static fields.
Leave a comment
There are multiple options to leave a comment:
- send me an email
- submit a comment through the feedback page (anonymously via TOR)
- Fork this repo at GitHub, add your comment to the _data/comments directory and send me a pull request
- Fill the following form and Staticman will automagically create a pull request for you:
1 comment
Thanks for this write up, I was able to find the information I was looking for in it… Great blogs I’ll be coming back frequently…