This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques and have Mario Casciaro & Luciano Mammino on-line!
See this thread for details.
Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Help needed in execution Sequence

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I have been looking another post where I came across a code...
I jsut added a superclass to it....

I am unable to unerstand the output sequence...

class zion
{
String temp=getString();
zion()
{
System.out.println("In zions constructor");
}
static String getString()
{
System.out.println("getstring");
return "string returned";
}
}


class Animal2 extends zion{
int i=InitialValue();
String name;
Animal2(String name){ super(); this.name=name; }

static{ System.out.println(" C's static" ); }

Animal2()
{ this(makeRandomName()); System.out.println("this"); }

static String makeRandomName(){
System.out.println("static");
int x=(int)(Math.random()*5);
String name=new String[]{"Fluffy","Fido","Rover","Spike","Gidi"}[x];
//System.out.println(name);
return name; }


public int InitialValue(){
System.out.println("Initial");
return 0; }


public static void main(String... args){
Animal2 a=new Animal2();
System.out.println(a.name);
Animal2 b=new Animal2("Zeus");
System.out.println(b.name);
}}


Output:

C's static
static
getstring
In zions constructor
Initial
this
Fluffy
getstring
In zions constructor
Initial
Zeus


I am unable to find how this output sequence came....

Please somebody help me to understand this..

Thanks
Praveen SP
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll try to explain things, il will be a little long:

1) You invoke the main method in class Animal2, so class Animal2 is loaded.
2) The JVM sees that Animal2 is extending zion, so class zion is loaded.
3) Static initialisers fron class zion are invoked: there are none.
4) Static initialisers from class Animal2 are invoked: "C's static" is printed.
5) You enter the main method.
6) new Animal2() is invoked.
7) The no-arg constructor wants to invoke the constructor that takes a String, but before that the makeRandomName() method is called in order to produce a random name.
8) In makeRandomName(), "static" is printed and a random name from the array is returned.
9) We enter Animal2(String name), where super() is invoked explicitly. Note that it would have been invoked anyway, even if not written explicitly. Also note that, because we didn't yet return from the super() call, no instance variable initialisation took place yet.
10) We enter zion(). First, super() is called implicitly (which is the no-arg constructor of class Object), then the instance variable initialisation for zion must take place.
11) As we said above, String temp is initialised, and because getString() is static it can be called and it prints "getstring". temp is initialised with "string returned".
12) The instance variables are initialised, we continue with the zion() constructor, which prints "In zions constructor".
13) We return to Animal2(String name), now the instance variable initialisation must take place, because we just returned from the superclass constructor.
14) i is initialised by calling InitialValue(), which prints "Initial" and initialises i to 0.
15) name is initialised to null.
16) We continue with the constructor, which assigns to name the random value calculated at points 7) and 8).
17) We return to the no-args constructor, which prints "this".
18) The object has been finally constructed, we return to main and print its name attribute (random name from the array of dog names).
19) new Animal2("Zeus") is invoked. Points from 9) to 15) are happening again for the new object creation.
20) name is assigned the value "Zeus".
21) Back to main, where we print the name atribute "Zeus".
22) End.

What I don't understand very clearly, is why at point 14) we are allowed to call InitialValue(), which is an instance method, even if the object is not yet constructed (we are still in the constructor).

Hope this helps,
[ August 03, 2007: Message edited by: Radu Zaharia ]
 
Praveen Seluka
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Radu ,


Thanks a lot for spending your time
explaining each and every point....

I got the same doubt,as how initialvalue() is called before object is created.
Doubt:

When will instance variable initializtion take place after entering constructor
Will all the instance variables get initialised before the constructor ends..

Please clarify

Thanks
Praveen SP
 
Radu Zaharia
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instance variable initialization will take place just after the return from the superclass constructor call. Then the constructor continues with the rest of its code. And yes, all the instance variables get initialised before the constructor ends.

For example, in the case of class Animal2, if we call the no-arg constructor, it will call the String constructor (in line //1), the String constructor will call the superclass constructor (in line //3), then the instance variable initialization will take place (between lines //3 and //4). After that, the constructor continues with line //4, returns to the no-arg constructor which continues with line //2 and only then we have finally constructed our object.

 
Praveen Seluka
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks radu

I got the point as when instance variables will be initialized...
What about that initial value method which is called before the object is created..
I got no idea how?


Thanks
Praveen SP
 
Something must be done about this. Let's start by reading this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic