Forums Register Login

An easy way to explain "Calling constructors from constructors"

+Pie Number of slices to send: Send
Hi,



I have just finished modifying Bruce Eckel's code to understand how "Calling constructors from constructors" work, so here it is, Enjoy







Now as you notice:



1. In the constructor, Flower(String s, int petals), if this(s); is used, then it basicaly calls Flower(String ss), and

System.out.println("Flower(String ss) " + ss); is printed



2. If the constructor, Flower(String s, int petals), this(petals); is used, then Flower(int petals) is called, and



is printed



3. Also we cannot have 2 this() in the same constructor



4. In print( ) you can see that the compiler won�t let you call a constructor from inside any method other than a constructor.



Hope this is helpful.

Thanks,

-Kamal.
+Pie Number of slices to send: Send
Does Bruce Eckel really write new String("null")?
+Pie Number of slices to send: Send
 

Originally posted by Jeff Albertson:
Does Bruce Eckel really write new String("null")?


Curiously, he does. :roll:
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 933 times.
Similar Threads
Constructor - Thinking In Java
Calling constructors from constructors ( A small problem)
Constructors
Constructor
Constructor - Thinking In Java
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 22:40:44.