• 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
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Object Initialization

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the following object initialization sequence correct?
1. Call to super() or this() Constructor.
2. Initialization of Static Variables.
3. Execution of Static Block.
4. Initialization of Instance Variables.
5. Execution of Instance Block.
6. Execution of rest of Constructor.
Thanks
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) When the class is loaded the static fields receive their default values (0 , 0.0 , null)
2) The class is initialized, typically, when the first instance is created, or the first access to one of its static members occurs. Then the static initializers and the static block initializers are executed in textual order.
3) Each time an instance of the class is created:
3.1) its instance fields receive their default values.
3.2) a chain of this(...) super(...) constructors calls end up into the constructor of the class Object.
4) For each constructor in the chain, from Object to the constructor of the class being instantiated, the sequence of execution is like this:
4.1) instance initializers and block instance initializers in textual order.
4.2) the code placed in the constructor by the programmer.
[ November 15, 2002: Message edited by: Jose Botella ]
[ November 15, 2002: Message edited by: Jose Botella ]
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Output:
Initing static variable A.i
Static initializer in A (super class).
Initing static variable B.b
Static initializer of class B (sub class).
Initing instance variable of A.ii
Instance initializer in A (super class).
Constructor of A (super class)
Initing instance variable of B.bb
Instance initializer of class B (sub class).
Calling the constructor of B (sub class).
Calling Constructor of C (sub of sub class B
 
There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic