Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

An easy way to explain "Calling constructors from constructors"  RSS feed

 
Kamal Ahmed
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does Bruce Eckel really write new String("null")?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Albertson:
Does Bruce Eckel really write new String("null")?

Curiously, he does. :roll:
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!