• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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

 
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.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does Bruce Eckel really write new String("null")?
 
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:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!