• 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Help with Understanding the Output

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The output comes out to be YXYZ Can someone explain how is this ?
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The output comes out to be YXYZ Can someone explain how is this ?



The instantiation order for objects are...

1. All instance variables (that are compile time constants) are done first.... Actually, this is done at compile time, but this is first nonetheless.
2. The super() portion of the constructor is called.
3. All instance variables are initialization, and instance initializers are called, in the order that they are encountered in the source.
4. The rest of the constructor is called.


So... if you apply this, you should get YXYZ.

Henry
 
Dhruv Arya
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didnt get the third point. Does it also initialize the Y class object declared in class X and that happens after the class X constructor has run
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dhruv Arya wrote:I didnt get the third point. Does it also initialize the Y class object declared in class X and that happens after the class X constructor has run



The y instance variable of X, will be instantiated, after the super() part of the X constructor, but before the rest of the constuctor.

Henry
 
Ranch Hand
Posts: 38
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:The y instance variable of X, will be instantiated, after the super() part of the X constructor, but before the rest of the constuctor.Henry


So shouldn't the answer be YYXZ?
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Valentin Ivanov wrote:So shouldn't the answer be YYXZ?



instantiate Z
---> super() of Z, ie. instantiate X portion
--------> super() of X, ie. Object portion
--------> variables of X, prints ................................. Y
--------> rest of X constructor, prints ....................... X
---> variables of Z, prints ....................................... Y
---> rest of Z constructor, prints ............................. Z


Henry
 
Dhruv Arya
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot Henry I get it now Thank You
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic