• 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

The default constructor

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic