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

Consturctor Invocation,

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please look at the following code. The output is YXYZ. But as per my knowledge, instance variables are created before constructor is called. So when Z() is invoked, a variable of Y is created and in return its constructor is called. Now when constructor of Z() is called, it first creates the parent class i.e X now before invoking its constructor it should create object of Y ie line number 3. and finally constructor of class x and hence z should be invoked. so the result should be YYXZ. Can anyone please explain me.


code :

[ December 26, 2006: Message edited by: Ritu Kapoor ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please edit your post and put tags around your program. That will preserve your code formatting and make it more readable.
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

The java compiler will interpret the code you have writen as below

class X {
Y b;
X(){
b = new Y();
System.out.print("X");
}
}

class Y {
Y(){
System.out.print("Y");
}
}

public class Z extends X {


Y y;
Z(){
y = new Y();
System.out.print("Z");
}
public static void main(String[] args){
new Z();
}}


so the output will yxyz not onley with respect to object creation but even with respect to assignments say if we have code like

public class edf{

int i=10;
int j;
String str=new String();
edf(){}
.....
}


the compiler will consider the above code AS


public class edf{

int i;
int j;
String str;
edf(){
i=10;
str=new String();
}
.....
}

Observe carefully that the order we wrote the code that is i,j, and str are maintained same in the constructor but the code at class level will be moved in to constructor only when we have definition if only declared then compiler will not move it inside in this case see int j it is not moved inside the constructor

Thanks
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ritu,
You are corrct that instance variable are created before constructor is called. But the way you described is wrong.
In your code when Z is created... Now before this x should be created. Now your rule (instance variable must be created first), so Y is the output then finally X is printed. Now control come back to class Z again same rule (instance variable must be created firstf) so Y is printed and again finally Z is printed.
This you can check it by commenting intance variable one at a time. Hope this might be useful to you

Sandeep Rana
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sandip,

Still im not clear can you just point out which Y is printed from which class(X or Z). As per my understanding it should be like the foll.

1. z() invokes y() [Prints Y]
2. then x() ---> x invokes y() [Prints Y]
x() [prints x]
3. then come back to z to print z
 
author
Posts: 23951
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

Originally posted by av bharathiraja:
Sandip,

Still im not clear can you just point out which Y is printed from which class(X or Z). As per my understanding it should be like the foll.

1. z() invokes y() [Prints Y]
2. then x() ---> x invokes y() [Prints Y]
x() [prints x]
3. then come back to z to print z



1. z() constructor calls it super constructor x()
2. x() constructor instantiates it y()
3. y() prints Y
4. x() constructor then prints X
5. z() constructor then instantiates its y()
6. y() prints Y
7. z() constructor than prints Z

Henry
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, the Z() will instantiate(new) all of its class classes before it completes its own constructor.

I did not know this.
 
Ritu Kapoor
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all of you especially Henry Wong.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic