aspose file tools*
The moose likes Java in General and the fly likes Constructor confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Constructor confusion" Watch "Constructor confusion" New topic
Author

Constructor confusion

Pradeep Kadambar
Ranch Hand

Joined: Oct 18, 2004
Posts: 148
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:
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60055
    
  65

"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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pradeep Kadambar
Ranch Hand

Joined: Oct 18, 2004
Posts: 148
Hi Bear,

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

Thanks.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

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++.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

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

Joined: Oct 18, 2004
Posts: 148
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

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 ]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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).


Tony Morris
Java Q&A (FAQ, Trivia)
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

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.


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Pradeep Kadambar
Ranch Hand

Joined: Oct 18, 2004
Posts: 148
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor confusion
 
Similar Threads
why runtime error?
Default constructor
quick class question
Default Constructor in Sub/Super Class