Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Overloaded constructors and this()

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone!

I'm reading Java Head First (pg 256), and am not understanding the stated use of this() for overloaded constructors. The scenario is that you have overloaded constructors that "all do the same thing", except they handle different argument types. So the use is to put a call to this() in all but one constructor and then put all the code in the 'real' constructor so you don't have to maintain the same code in multiple constructors. Ok, so............

I thought there might be some minor code in each constructor which 'massages' the parameters so they fit into the one main constructor then you call it with this(args). However since this() has to be the first thing in the constructor, I'm not seeing how that scheme is useful at all. I seem to be completely missing the point. Can I get some clarification?

Thank you,
Paul
 
Marshal
Posts: 28288
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wasn't there an example to go with that? I'm surprised to hear that. Anyway...
 
Marshal
Posts: 79633
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All constructors do the same thing (or at least they ought to): getting the object into a consistent state (or establishing up its invariants). Imagine you have several constructors which all set up the object's invariants, but take different numbers of arguments, and fills in the gaps with default values. You can avoid duplicated code by the use of this(). Like "this":As you see, this saves you writing seconds = 0; thrice, which might have been a maintenance pitfall. The path of control from all four constructors ends up at the 3-arguments constructor, where the assignments are carried out.
You can tell the Instrument class has a constructor which takes one String argument, eg "Clock", "Ammeter", or "Micrometer".
 
Paul Berry
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies!

OK, I can see how several different argument types can be handled, but....

What if the argument is not exactly what it needs to be (like an int instead of a float), or some other sort of manipulation/decision needs to happen *before* passing it down the line to another constructor? How would I have code that processes the arguments passed *before* calling this() ?

It seems that there is a deliberate design to keep that from happening. Is it just bad programming practice (like I should always process arguments before using "new"), or will something really bad happen if I could have code before "this()" ?

 
Campbell Ritchie
Marshal
Posts: 79633
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Beware of different types for constructors. If your field is a float (well, a double would probably be better), you can get away with automatic casting, so 123 becomes 123.0f. But for other things, you should be restricting the constructor. Remember the constructor tells users what type of data you requrie for your class, and you shouldn't let people try to squeeze the wrong things in.
 
Paul Berry
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should say that there was an example in the book, but it was short and targeted to make their point and didn't help with my supplimental question.... super() or this() have to be first, but it didn't say why, or address running code prior to.

I get the impression that it would be bad style to put a lot of additional code in each overloaded constructor. Probably a maintenance nightmare to follow a bunch of branches after you call new. Ask for a new instance and just do what is necessary to get that new object instead of branching all over the place during object creation.

Thanks for the help!
 
Paul Clapham
Marshal
Posts: 28288
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Berry wrote:I should say that there was an example in the book, but it was short and targeted to make their point and didn't help with my supplimental question.... super() or this() have to be first, but it didn't say why, or address running code prior to.



Having worked in languages where the call to the super() constructor didn't have to be the first thing in a constructor, I would say that it was an arbitrary decision made by the designers of Java. No doubt they had a good reason for that decision, but possibly the designers of Object Pascal had a good reason for making a different decision.

Anyway the bottom line is that it's extremely difficult to get any work done before calling this() or super() in Java. It's just something you have to deal with if the requirement arises.
 
reply
    Bookmark Topic Watch Topic
  • New Topic