*
The moose likes Beginning Java and the fly likes Constructor and Class Loading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor and Class Loading" Watch "Constructor and Class Loading" New topic
Author

Constructor and Class Loading

Nilesh Srivastava
Ranch Hand

Joined: Aug 29, 2003
Posts: 70
Hi Can anyone tell me while loading a class what is the sequence of execution.
Here is a snippet. plz tell me howz this output is coming.

Please explain me in detail as I have referred so many books that I got confused on this topic.
Why "Happy Actor" is coming before "Stage" ?
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ August 30, 2003: Message edited by: Dirk Schreckmann ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Nilesh
Stage's constructor creates an object of type HappyActor. Then you call go() method of the construcor which in turns calls the act() method of the HappyActor and then the actor changes and the same sequence is repeated.
Nilesh Srivastava
Ranch Hand

Joined: Aug 29, 2003
Posts: 70
HI Anupam ,
Thanks for replying but I still stuck on the same very point that why is HappyActor is printed before Stage.
When Stage is instantiated then it will call Stage's constructor and it should print "CONST ::: Stage" on the console.Instead it is executing
Actor a = new HappyActor();
and after executing the HappyActor's constructor it prints "CONST ::: HappyActor" then it is calling Stage's constructor.
According to me the sequence of exection should be
1)Stage's constructor will excute first
2)then HappyActor
3)atlast the sadActor's constructor should run
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
That's the way it is specified in Java (according to http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#44670 ):
When you call a constructor, first the super-constructor is called, then *all instance variable initializations are executed* and only then the constructor body is executed.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Tom Hill
Ranch Hand

Joined: Aug 24, 2003
Posts: 115
Can i suggest changing:

??
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ August 30, 2003: Message edited by: Dirk Schreckmann ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Nilesh
I misunderstood the question earlier. I thought that you were asking as why at all Happy actor comes to the stage.
Ok the HappyActor comes before Stage because the constructor of the HappyActor is being executed before that of Stage's.
Look here


Now before the constructor is executed the variables that have been declared are intialized. Think of it this way, the use of the constructor is to intialize the variables. So before the constructor can be executed line no. 3 is executed. Which declares a variable a of type actor type and intializes it with a reference to an Actor type of Object. To get an Actor reference an Actor type object is created for which the Actor's constructor is executed.
Follow this link to know more about it.
Tom Hill
Ranch Hand

Joined: Aug 24, 2003
Posts: 115
I justtook a few minutes to show the execution order - the two things i said earlier probably arnt appropriate!
2 files: JavaCheck.java and JavaChecker.java
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ August 30, 2003: Message edited by: Dirk Schreckmann ]
Nilesh Srivastava
Ranch Hand

Joined: Aug 29, 2003
Posts: 70
Hi All,
Thanks you all for the help.
Anupam U specially u cleared my doubts.
Once again thanks a million to u all
Nilesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor and Class Loading
 
Similar Threads
private constructor
what wrong with my code?
Completed SCJP with 93%
How to access this interface?
Can anyone explain this please ??