• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why no default constructor when one with parameters is declared?  RSS feed

 
Devang Kamdar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Firstly the fact - Java does not provide a default ctor if we explicitly define a parameterized ctor in a class.

Assuming this fact is true, Any idea why so?
I mean why this JAVA spec was implemented or what is the reasoning/logic behind this?

A very curious java programming beginer colleague asked me this question and thought it would be interested to know really why is it like how it is?

Please let me know if this is not the right place to ask this type of question and if I should post this to any other forum?

Thanks
Dev.
[ August 04, 2008: Message edited by: Bear Bibeault ]
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is a "ctor"? Please use standard language. This isn't IRC.
 
Devang Kamdar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry My Bad!
By ctor I meant constructor.

Actually the title was not allowing beyond certain characters, so I improvised ... but I will keep that in mind in future.

Thanks for pointing out.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class has to have at least one constructor, therefore you automatically get a no-arg one if you don't declare one. But if you do declare your own constructor and the compiler still added a default no-arg one, what would you do if you didn't want a no-arg constructor ?
 
Devang Kamdar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Few of the cases from top of my head:
1. If say there is a class derived from this class, and the subclass does not call the super() with the proper parameters, then a default constructor called by Java will still allow the program to compile.
2. If we want to iniitalize the class and the arguments are not passed, then a default constructor call would do the job.

Both of the above are actually not ideal way of coding. But Java does save the day when there is no parameterized constructor defined. So why does it not use (provide) the implicit no-arg constructor when we have declared one? Just curious to know the design reason behind this decision? Are we violating something or inviting trouble when expecting Java to cover up in such cases? If so what kind of trouble?

Thanks.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joanne Neal:
But if you do declare your own constructor and the compiler still added a default no-arg one, what would you do if you didn't want a no-arg constructor ?

Declare it yourself as private could work.
 
Devang Kamdar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:

Declare it yourself as private could work.


I am sorry I did not address your question Joane earlier i interpreted it all wrong. But that is a good point. And the reply sounds good too ... Thanks Rob ... So would that be the reason, JAVA architects decided to do what they did?

Is there a way to find out or will have to bury under the stack of Java Specifications and/or JSRs (Java Specification Requests)?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:

Declare it yourself as private could work.


That would stop the compiler automatically adding a no-arg constructor, but it would seem a little strange to have to declare a private no-arg constructor in order to stop the compiler adding a public no-arg constructor.
[ August 04, 2008: Message edited by: Joanne Neal ]
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joanne Neal:


That would stop the compiler automatically adding a no-arg constructor, but it would seem a little strange to have to declare a private no-arg constructor in order to stop the compiler adding a public no-arg constructor.

[ August 04, 2008: Message edited by: Joanne Neal ]


Usually, from what I've seen, a private no-arg constructor is useful in a Singleton class to negate developers from possible getting a new version of the object rather than calling the typical getInstance() method.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as finding out why Sun elected to do it the way they did, good luck. For a lot of things I decided a long time ago to try and stop questioning Sun (non)logic. I often sleep better at night.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joanne Neal:
That would stop the compiler automatically adding a no-arg constructor, but it would seem a little strange to have to declare a private no-arg constructor in order to stop the compiler adding a public no-arg constructor.

Don't get me wrong - I agree with you. It's just how it could be done if necessary

I've only used private constructors
a) in utility classes that only have static utility methods
b) in singleton classes
c) if needed internally for creating instances but those constructors cannot be public / protected
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Devang Kamdar:
Actually the title was not allowing beyond certain characters, so I improvised
I adjusted the topic title with an improvisation that doesn't resort to fake words.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!