• 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

Constructor confusion

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Someone threw a question at me. Why there will not be a default constructor in a class if explicit constructors with arguements are defined?

Well my explanation was :

If there are not explicit constructors then the intension here is just to create the object.

If explicit constructors are defined with arguments then the intension is to initialize. So if default constructor were to be there then it will defeat the cause.



Is this the right justification? Or there are any particular/more significant reasons? :roll:
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Pradeep KG",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff
 
Pradeep Kadambar
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bear,

With due respect, my full name is 'Pradeep KG' were 'KG' is my initials. I use this name for all purposes.

Thanks.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why there will not be a default constructor in a class if explicit constructors with arguements are defined?

Because you don't always want a public, no-arguments constructor. If the compiler would always generate a public, no-args constructor, then it would be very hard to implement the singleton design pattern in Java, because a class couldn't control the creation of instances very easily.

You should look at it the other way around: The fact that the compiler generates a public, no-arguments constructor is a feature of the compiler to save you some typing. This is one of the many features that Java inherited from C++.
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

The java will not provide a defualt constructor If, any user defined constructor is present. The reason itself is present in above statement. Defining constructor means user want to declare a particluar type of object of that class.

Constructor is way to define the initial behavior of the object. you are defining the behavior. The whole object control is in your hand. Think of a situation, where you want to to declare a object which always takes two inputs in creation to decide its state, but some has created with out any input. The whole purpose of your class will go wrong.

So the constructors are used to define the initail behavior of the object.

So If you have atleast one way to define, then you are the control of the remaing ways too.

And If you do not provide any way ,then java will provide one way to set initial behavior to the object.

Hope, I have given enough information.

Please add if you know more ......



 
Pradeep Kadambar
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This makes me think that without default constructor I won't be able to instantiate the class using reflection. Right??

Then even if I don't intend to provide no argument constructor I need to provide one?

:roll:
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This makes me think that without default constructor I won't be able to instantiate the class using reflection. Right??

No, wrong. You don't need a default constructor to be able to instantiate a class using reflection. Here's an example.

[ August 31, 2006: Message edited by: Jesper Young ]
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that a "default constructor" and a "niladic constructor" are different things. These terms are often erroneously used interchangeably as is the case in this thread. A default constructor is always implicit. An explicit no-argument constructor is not a default constructor. The default constructor's access modifier is always implied by some governing rules. An explicit no-argument constructor may have an explicit access modifier or an implied access modifier if none is provided (another common misconception is that this implication is always public).
 
Rancher
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Morris:
Note that a "default constructor" and a "niladic constructor" are different things.
(...)
An explicit no-argument constructor may have an explicit access modifier or an implied access modifier if none is provided (another common misconception is that this implication is always public).



Insightful.

Could you describe the rules that specify the access modifier for the default constructor and the implied case for the "niladic" (*) constructor?

- Anand

(*) Had to look that up.
 
Pradeep Kadambar
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tony very useful piece of information ....

Partly to blame the so called bibles of Java do not make these things clear. It often in forums such as the ranch that makes one proficient.

 
You guys haven't done this much, have ya? I suggest you study this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic