Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

The default constructor  RSS feed

 
Ranch Hand
Posts: 43
  • 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.
 
Bartender
Posts: 4568
9
  • Likes 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.
 
Ranch Hand
Posts: 84
  • 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.

 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • 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.
 
Greenhorn
Posts: 14
  • 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.
 
Marshal
Posts: 60065
188
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!