Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The default constructor

 
Monica Marcus
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Is a default constructor just another name for a no argument constructor? Or are they similar though distinct notions? On the Oracle site I found in a Java SE 7 documentation the definition of default constructor, and they don't mention there the no argument constructor (I mean in the same subsection.)Chapter 8.8.9 Default Constructor
However, in a book by Bruce Eckel I find that the two notions are synonyms. The answer is very important to me because of a question from the Java certification test.

Thanks a lot.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are often mistakenly used in the same way, but they aren't quite the same.

The default constructor is the constructor that is inserted by the compiler automatically if no other constructor is provided. This is always a no-argument constructor. But if you provide your own no-argument constructor then that isn't a default constructor.
 
Ranji Sura
Ranch Hand
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are often mistakenly used in the same way, but they aren't quite the same.

The default constructor is the constructor that is inserted by the compiler automatically if no other constructor is provided. This is always a no-argument constructor. But if you provide your own no-argument constructor then that isn't a default constructor.


If you look at the meaning of "Default" closely.. you can understand why ?

This is definition for the default in oxford dictionary : - "( computing ) what happens or appears if you do not make any other choice or change "

I think , You have already found the answer yourself, if you have really studied the Default Constructor

If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Ranjith : Nice to see a guy who once himself had doubts about method local-inner classes only being able to access final variables of the method answering another person's simple query about constructors.
 
vinay chaturvedi
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Default constructor is the constructor that is inserted in a class by the compiler if the compiler doesn't find any constructor in the class at the time of compilation of the class. This default constructor inserted by the compiler is a no-argument constructor.

In the case when a user defines a no-argument constructor in a class then the compiler doesn't insert its default constructor in the class. Hence A default constructor is always a no-argument constructor but a no-argument constructor may or may not be a default constructor.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown’s answer tells it all.
A lot of people believe default constructors are poor programming style and you should always write a constructor.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic