• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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:
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!