Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Class instantiation process  RSS feed

 
Golla Vensiri
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Can any one give the output for the following program and also explain me what things will happen at the time of class loading and instantiation.

class X
{
Y b = new Y();
X()
{
System.out.print("X");
}
}
class Y
{
Y()
{
System.out.print("Y");
}
}
public class Z extends X
{
Y y = new Y();
Z()
{
System.out.print("Z");
}
public static void main(String[] args)
{
new Z();
}
}
 
Niki Nono
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you instantiate the Z class its parent is called which is X.
the instantiation of Y takes place in X and that is why 'Y' is printed.
then the constructor of X gets executed which prints 'X'.
Then control comes back to Z class which executes its Y class statement which prints 'Y' again and then control moves into the Z constructor.

read the aboev sentences carefully and i hope you get what i am trying to convey.
let me know if you have any queries.
 
Ken Loh
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Golla Vensiri:
Hi

Can any one give the output for the following program and also explain me what things will happen at the time of class loading and instantiation.


"YXYZ" is my vote.
 
venkat venkataraman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ken,
I am a newbie to java, so bear with me if its a silly question. When I executed the code I also got "YXYZ", but I expected it to be "YYXZ" because of Y y = new Y() statement before Z() in the Z class.......
 
Golla Vensiri
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Niki,

Thanks a lot
i got the thing
But one doubt is that ' as of my knowledge when the constructor is exceuted for a class fist the constructor of super class is called' So according to this i thought the ans to be 'YXYXZ'. This is because before excuting the constructor of z super class constructor is called

Can you explain me the steps that will take place when the class is loaded first into the memory and also when an Object is created for a class

Thanks
Niki
 
Niki Nono
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
golla

when you say that the constructor is executing doesnt that mean that the class is being instantiated???
they are the same thing.
and you are right when you say that the constructor of super class is called first.
thats why you get X in the second place. It has already happened.

So in your main method when you call the class, parent gets called first wich calls Y and then child gets called which calls Y again.

got it???
 
Ken Loh
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The attribute of an ancestor class is initialized when a descendant class (meaning even many, many levels down) is instantiated. It does not wait for its class's constructor to be called.

Originally posted by venkat venkataraman:
Hi Ken,
I am a newbie to java, so bear with me if its a silly question. When I executed the code I also got "YXYZ", but I expected it to be "YYXZ" because of Y y = new Y() statement before Z() in the Z class.......
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!