• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Gratuitious constructor

 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From C++ I remember Meyers rule "Avoid gratuitious constructors". I forgot why? What terrible things may happen?

Vladas
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am interested in Java context... Are these still bad in Java?
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vladas Razas:
From C++ I remember Meyers rule "Avoid gratuitious constructors". I forgot why? What terrible things may happen?

Vladas



http://home.earthlink.net/~huston2/dp/init.html
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no initializer lists in Java. In C++ there are reasons to avoid gratuitious constructors, but how about Java? Aren't we still typing

for no reason?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd see it more a design decision than a blanket statement. If you create an object via the default constructor, is it truly useful yet? Or is it in an invalid state because some member variables are not yet filled in? The answers are both "it depends." You'll have to answer this on a per-object basis.

There are a lot of benefits to immutable objects that are fully initialized by the constructor and have no methods to change any state. Search for past discussions of immutable String for more details.
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I dont argue about constructor's purpose I am just asking if explicitly typing default constructor with no params and no functionality is "a good" style in Java as it was in C++ .
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"From C++ I remember Meyers rule "Avoid gratuitious constructors". I forgot why? What terrible things may happen?"

Actually, the rule is "Avoid gratuitous default constructors."

Briefly, Meyers's reasoning is that an object may not have all the information to be fully initialized if the object is constructed through its default constructor. For example, an Employee class may take an EmployeeID as a constructor argument. The Employee object is really incomplete without that EmployeeID. In this context, I think the "avoid gratuitous constructors" rule also applies to Java.

The term "default constructor", however, opens up a hole in another can of worms when applied to Java. Java programmers seem to use the term "default constructor" to mean only the compiler gernerated constructor.
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if my class is fully initialized and ready to use without any special steps, then it's not bad if I do not write any constructor?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vladas Razas:
So if my class is fully initialized and ready to use without any special steps, then it's not bad if I do not write any constructor?



Exactly.
 
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
J Borderi:

Actually, the rule is "Avoid gratuitous default constructors."

Briefly, Meyers's reasoning is that an object may not have all the information to be fully initialized if the object is constructed through its default constructor. For example, an Employee class may take an EmployeeID as a constructor argument. The Employee object is really incomplete without that EmployeeID. In this context, I think the "avoid gratuitous constructors" rule also applies to Java.


Right. And in Java, if you write no constructor at all, that means there is a default constructor that may or may not be gratuitous - people can still call the automatically generated one even if you haven't written it. For that reason, I think it's best to explicitly write the default constructor if you intend for it to be used - and for safety, if the compiler will generate a default constructor that you don't want used, write it and make it private to enforce that.
 
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler will not generate a default constructor if you write your own constructor (with or without parameters).
 
What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic