This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic