• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Which happens first?

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both, a Sun instructor I had and Whizlabs say that when an object is constructed ("new"), default and explicit initialization of variables happens first, then the constructors are executed.

K&B, page 128 says something different. Step three on this page says that the constructor is invoked and then step four says instance variables are given their explicit values. Then step five says the constructor completes, indicating that variable initialization occurrs durng execution of the constructor.

Can someone clarify please, does it matter or am I splitting hairs here?
thanks
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think they are actually saying the same thing. "Invoking" a constructor basically just means calling it. Instance variables can't really be initialized before this, because no one knows there's going to be an instance until the constructor is called. So after the constructor is invoked (and after constructors of super classes all finish), then instance variables are initialized before the remaining body of the constructor completes.
[ June 08, 2007: Message edited by: marc weber ]
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both, a Sun instructor I had and Whizlabs say that when an object is constructed ("new"), default and explicit initialization of variables happens first, then the constructors are executed.

i hope they say instance variables are initialized after executing super constructors and before completing the constructor in the class .i mean after super constructors and before actual constructor in class.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To observe the order of initialization, try this...
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

i have extended the example to include initialization blocks.

 
Gary Marshall
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Marc and Manfred. Your coding example has illustrated that the following occurs in the order shown:
1. Static instance variables of super class are assigned
2. Static instance variables of sub class are assigned
3. Instance variables of super class are assigned
4. Super class constructor runs
5. Instance variables of sub class are assigned
6. sub class constructor runs
This makes sense. First assign the class-level variables of both class within the heirarchy. Then finish off assigning other variables of the super class AND THEN run the constructor of the super class. Then do the same for the sub class. As I said it makes sense and it looks like the JVM is doing allot of bouncing around trying to get everything initalized and ready to go.

Thanks
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Superb program to explain the flow Manfred.

Marshall had collected the information in a nice way!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic