wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Initialization Process Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Initialization Process" Watch "Initialization Process" New topic
Author

Initialization Process

Marcus Moreno
Ranch Hand

Joined: Aug 17, 2008
Posts: 58
Hi,

studying for the SCJP, I had some problems in understanding the rules of the class loading and object creation process. To entirely understand it, I wrote a little test programm, which imho answers all open questions.
I'd like to share it with you, in the hopes that it will also help you.
(Please let me know what you think...)


The output is:

begin
End variable declaration
------------------------------
static variable A1
static initializer A2
static initializer A3
static initializer A4
static initializer A5
static variable A6
static initializer B1
static variable B2
static initializer B3
static initializer B4
static initializer B5
static variable B6
------------------------------
End static initialization of B
------------------------------
------------------------------
End static initialization of A
------------------------------
instance member A1
instance initializer A2
instance initializer A3
instance initializer A6
instance initializer A7
instance member A8
instance constructor A5
instance constructor A4
instance member B1
instance initializer B2
instance initializer B3
instance initializer B6
instance initializer B7
instance member B8
instance constructor B5
instance constructor B4
------------------------------
End instance creation of B
------------------------------
instance member A1
instance initializer A2
instance initializer A3
instance initializer A6
instance initializer A7
instance member A8
instance constructor A5
instance constructor A4
------------------------------
End instance creation of A
------------------------------

From the output I come to the following conclusions:

1. A class is loaded only when it's used, not just because it's part of the file where the main method is executed.
2. A class is loaded, as soon as an instance is is created, or if you access a static member of the class.
3. You have to look at the process in different levels of abstraction.
First level: The classes - Parent class is loaded before child class
Second level: The variables and initializers - static before instance, each "group" from top to bottom, first in class is loaded first
Third level: The constructors - The last thing beeing called are the constructors. The order in which they are called depends on how they are chained.
4. static variables / initializers are loaded only once, instances variables / initializers are loaded once for each instance created.

It's as easy as this. That's imho all you got to know!
Reading the SCJP book of Kathy and Bert, so far I never understood it in this detail, and failed in all mock test on the subject.

Now with some coding, I think I finally understood.

Did I get it right???

Marcus
[ October 18, 2008: Message edited by: Marcus Moreno ]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Thanks Marcus for sharing your knowledge...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
sumi rankan
Ranch Hand

Joined: Apr 07, 2008
Posts: 46
Hey Marcus,
The code of yours sure did show some nuances in the initializing process.Thanks for sharing with us.Now we know why you take >15 months to
take a shot at scjp.Wish you all the best!
N Sam
Ranch Hand

Joined: Nov 23, 2013
Posts: 77
    
    2
Thanks for posting this code. I have a question on line 82 & 83:


This seems to declare a static variable called myStaticB, but its type is B. Coming inside the declaration of class B, this seems to have a recursive definition. Obviously this is syntactically correct, but the recursive look is causing confusion to me. Can someone explain this piece of code ?

The other line of code that confuses me is line 26


How come this call to the constructor ends up calling 2 constructors of class A ? The printout shows

I can understand all the output until then, and i see that first this code would call public B(string s). How come the empty constructor of A gets called from here ? Is this because of extending the base class ? I am not upto reading about inheritance in java , but understand the basic principles of inheritance.
N Sam
Ranch Hand

Joined: Nov 23, 2013
Posts: 77
    
    2
Are the moderators & others reading this thread at all ? No answer in many days, and i hate to create a separate thread again for this topic.
John Wilson
Greenhorn

Joined: Dec 04, 2013
Posts: 3
N Sam wrote:Thanks for posting this code. I have a question on line 82 & 83:


This seems to declare a static variable called myStaticB, but its type is B. Coming inside the declaration of class B, this seems to have a recursive definition. Obviously this is syntactically correct, but the recursive look is causing confusion to me. Can someone explain this piece of code ?


You may be over thinking this one. It is just declaring myStaticB as a static reference variable of type B. It is probably just in there to demonstrate when static variables get initialized. It only gets used on line 11: where it is assigned to a local reference variable, which, unless I'm wrong, doesn't really do anything, since myStaticB would initialize with a default value of null, b1 was explicity declared as null, so assigning myStaticB to b1 they are both still null. See, confusing. Just understand that it is initializing a static reference variable of type B.

N Sam wrote:
The other line of code that confuses me is line 26


How come this call to the constructor ends up calling 2 constructors of class A ? The printout shows

I can understand all the output until then, and i see that first this code would call public B(string s). How come the empty constructor of A gets called from here ? Is this because of extending the base class ? I am not upto reading about inheritance in java , but understand the basic principles of inheritance.


It's all about the constructors. Calling the B constructor that takes a string "b", calls this() for the no-arg B constructor, which the compiler will insert super() as the first line since there is no super() or this() already there. So super() calls the superclass constructor, in this case the no-arg constructor of A, which has a this("s") which calls the A constructor that takes a string. Once you study constructors more it will make more sense.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Initialization Process