• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

What is the point of constructor chaining?

 
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On page 201 in the book 'Certified Associate Java SE 8 Programmer I' by Jeanne Boyarsky and Scott Selikoff, given the code sample:



What is the point of using constructor chaining? I can understand the overloading constuctors being used like this:



That makes sense, because you might want to create an object with only some of the parameters. But why would constructor chaining ever be useful?
 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure line 15 isn't
 
Sam Peterson
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:Are you sure line 15 isn't



Yes it is. Sorry. I was experimenting before posting. Why would we ever want to use constructor chaining?
 
Knute Snortum
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you wrote has a lot of repeated code and some constructors leave fields without suitable default values.  What if you had to change a field?  Now you have to find all the constructors that use that field.  With chaining, everything points to the same code as well as setting some reasonable default values.
 
Marshal
Posts: 27368
88
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Peterson wrote:That makes sense, because you might want to create an object with only some of the parameters. But why would constructor chaining ever be useful?



No, that's not what your code does. An object always has all of the instance variables declared within it. The idea that the object can only have "some of the parameters" isn't how objects work.

What your code does is, some of the constructors fail to initialize some of the instance variables. There's nothing wrong with that, all that happens is that the instance variables which you don't initialize remain equal to zero or null.

However the example in the book works differently. If you use a constructor which doesn't initialize all of the instance variables, you'll notice that the variables which aren't initialized via constructor parameters are initialized to non-zero values via constant values in the constructors. You could think of those as default values if you liked.

You could certainly modify your code to do that without constructor chaining. Like this:



But that has the problem of duplicate code. If you want to change the default for numWhiskers to be 7, you've gotta change it in two different places. And duplicate code is a serious sin in Java.
reply
    Bookmark Topic Watch Topic
  • New Topic