• 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

constructor chaining

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


output

C's static
static
Initial
this
Gidi
Initial
Zeus

Here no-arg constructor runs before arg constructor which is conflict with my yesterday post.link

Yeming or chandra can you give me any hint?
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is good question Dolly!
Hope this will help you to understand now!!!
Lets trace the code line by line Hope this will help you?

public static void main(String... args){ //1
Animal2 a=new Animal2(); //2
System.out.println(a.name); //3
Animal2 b=new Animal2("Zeus"); //4
System.out.println(b.name); //5
}

As soon as this class is loaded in the JVM
static block will be loaded
(This is static so it will be loaded one time for the JVM not for every object created for this class)
so
System.out.println(" C's static" ); is execute and print
C's static
line //2 is execute and it call constructor Animal2()which call makeRandomName() which first call System.out.println("static"); then print
static
Then it will load the
Variable I for the object(This is non-static so it is loaded for object not for the class)
int i = InitialValue();
Which execute method InitialValue();
Which will execute System.out.println("Initial"); and print
Initial
We are still in the constructor Animal2()Then it will execute
System.out.println("this"); and print
this
Now we are out of constructor and in line 3
which will execute
System.out.println(a.name);
Since this statement is already executed in makeRandomName() method
String name = new String[] { "Fluffy", "Fido", "Rover", "Spike", "Gidi" }[x];
It will print
Gidi
Because Gidi is the last value in the array.

Now we are in the line //4
So it will call constructer
Animal2(String name)
Which will assign name = �Zeus�

Then it will load the
Variable I from the class
int i = InitialValue();
Which execute method InitialValue();
Which will execute System.out.println("Initial"); and print
Initial
Next it will be in line //5 which will execute
System.out.println(b.name); and print
Zeus

[ August 02, 2007: Message edited by: Aaron Raja ]

[ August 02, 2007: Message edited by: Aaron Raja ]

[ August 02, 2007: Message edited by: Aaron Raja ]
[ August 02, 2007: Message edited by: Aaron Raja ]
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dolly

Here no-arg constructor runs before arg constructor...



Animal2 a=new Animal2();
This line will call the no-arg constructor which calls the makeRandomName() and then with the returned string value it will call the arg constructor. After completing the arg constructor, no-arg constructor will do the rest of its own. Just put a println()in the arg constructor. I hope you will get the point easily.
[ August 02, 2007: Message edited by: Al Mamun ]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it runs first only when you call it:

new Animal2("Louis") will run the arg-constructor first
new Animal2() will run the non-args constructor first

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

I really dont know at what point you all are confused.

Look only one constructor/per class in hirarchy executes untill and unless you explicitly mention this call in your constructor.

And that one constructor depends on your object initialization call that is new <YourClassName>().
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic