• 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

Doubt on Default Constructor!!

 
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

I am seeing last post about the constructors. I got confused.
If the system provides the no-argument constructor, then its called default constructor. for example we provide the following constructor, with no argument.
Example:
public Cons(){}

The above construcor is called default constructor??
Please clear my doubt.
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your provide no parameter constructor, then it is not a system provided constructor. It is programmer specified constructor.

If programmer do not specify any constructor, then system will provide default zero parameter constructor.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The sytem will only provide the default constructor if there are no constructors defined in your class. In your example, you have declared a constructor "Cons", therefore the default constructor won't be added. (Please note, that "Cons" is not called a default constructor, you have explictly declared it). Hope that helps.
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This thread seems to be either a continuation or another conversation of the following thread: http://www.javaranch.com (see my post for the case for any no-arg constructor).

Briefly, we all agree that in Java the compiler supplied no-arg constructor is called a default constructor. Some programmers refer to any no-arg constructor as a default constructor.

I know that other languages, notably C++, refer to any (that is, either compiler created or programmer coded) no-arg constructor as a default constructor. The usage for both is firmly rooted in C++ programmers' jargon and in the literature.

The debate is whether it is improper in Java to refer to a programmer defined no-arg constructor as a default constructor. I have not seen anything definitively excluding (or including for that matter) calling the no-arg constructor a default constructor in Java. My Java knowledge and library, however, are nowhere near so deep as my C++'s.

Since this is the SCJP Programmer Certification board, we might ask why this question is relevant. It is relavent only if the SCJP exam asks a question similar to the one in the post cited. Given the ambiguity or, more aptly, lack of definitiveness of the meaning of default constructor in this context, I don't believe the SCJP should ask that question. We are programmers; we are not language lawyers.

Still, for completeness I would like the definitive answer.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the way it is explained in the book I am studying from.(Which I highly recommend by the way as it is great):

"A no-arg constructor is not necessarily the default constructor, although the default constructor is always a no-arg constructor. The default constructor is the one the compiler provides. While the default constructor is always a no-arg constructor, you're free to put in your own no-arg constructor."

p. 315, Sierra, Kathy and Bates, Bert. Sun Certified Programmer & Developer for Java 2 Study Guide. McGraw-Hill/Osborne 2003.

If it's in this book, I assume it may be on the exam. I think default means at compile time default, not at run time default. In other words, at compile time if you do not declare ANY constructors, the compiler will add this one at compile time because every class must have at least one constructor:




I think it is important to distinguish between the default constructor and a no-arg user defined constructor because the default constructor will ALWAYS call the no-arg constructor of the parent (whether or not one trully exists) while a user defined no-arg constructor may be configured to call any constructor of a parent class. For that matter, a user defined no-arg constructor may have other code in it as well. In other words I can say for sure that the default constructor has not only no-args, but also has a certain predetermined default behavior, while a user defined no-arg constructor may not.

That's just how I read it. I'm still learning too.

[ November 13, 2004: Message edited by: J Williams ]
[ November 14, 2004: Message edited by: J Williams ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic