wood burning stoves 2.0*
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
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: 4343
    
    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.
Ranjith Suranga
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: 38003
    
  22
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: The default constructor
 
Similar Threads
explaination about exception
Default constructor
Default constructor
What's the difference? (or is there one?)
Constructor