• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Initialization Process

 
Ranch Hand
Posts: 63
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marcus for sharing your knowledge...
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 77
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
You showed up just in time for the waffles! And this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic