File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes The default constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The default constructor" Watch "The default constructor" New topic
Author

The default constructor

Monica Marcus
Ranch Hand

Joined: Oct 17, 2012
Posts: 43
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

Joined: Apr 06, 2010
Posts: 4421
    
    8

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

Joined: Oct 28, 2012
Posts: 84
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

Joined: Jul 27, 2010
Posts: 1157

@ 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.


~ Mansukh
vinay chaturvedi
Greenhorn

Joined: Jan 16, 2012
Posts: 14
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

Joined: Oct 13, 2005
Posts: 39396
    
  28
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
 
subject: The default constructor