Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor and Class Loading

 
Nilesh Srivastava
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tom Hill
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Thanks you all for the help.
Anupam U specially u cleared my doubts.
Once again thanks a million to u all
Nilesh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic