File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "constructor" Watch "constructor" New topic
Author

constructor

Vineela Devi
Ranch Hand

Joined: Dec 20, 2003
Posts: 191
Hi,
A very basic doubt.
Is the no argument constructor same as default constructor?
Vineela
Sanjay Gandham
Greenhorn

Joined: Mar 26, 2004
Posts: 8
Not quite. As per what I understood, only the compiler provided no-arg constructor is referred to as default constructor.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi Vineela and Sanjay,
Either way no-arg constructor is the default constructor.
Regards
Maulin
Sanjay Gandham
Greenhorn

Joined: Mar 26, 2004
Posts: 8
Maulin,
That's not what I read, even though it's more of a linguistic/semantic kind of discussion whether it should be this way or not, here is an excerpt from K&B (Page 315, last paragraph) :-
A no-arg constructor is not ncessarily 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.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
A no-arg constructor defined by you is not necessarily the same as the "default" constructor you'd get if you didn't define a constructor. You can read about it in the JLS, §8.8.7 Default Constructor, but here are a few things to keep in mind.
1. The default constructor takes on the same access modifier as the class. Therefore, a public class would have a public default constructor. If you were to define a public class with a no-arg private constructor, you'd have a different no-arg constructor.
2. The default constructor has no throws clause. If you were to define a no-arg constructor that could throw an exception, it would be different from the default constructor.
3. And, of course, the default constructor is special in that it is provided for you, unlike any constructor that you would define.
I hope that helps,
Corey


SCJP Tipline, etc.
rong zh
Greenhorn

Joined: Mar 28, 2004
Posts: 5
if u didn't define a constructor, the default constructor is no-arg one.
if u have already defined a constructor, there is no default constructor


<a href="http://www.djtz.net/get.asp?get=10941" target="_blank" rel="nofollow">mock exam collection</a>
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi Corey,
Thanks for the clarification.
I overlooked the "linguistic/semantic" part in the question
Thanks
Maulin
Vineela Devi
Ranch Hand

Joined: Dec 20, 2003
Posts: 191
Hi Corey,

Thanks a lot for ur explanation.
Vineela
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: constructor
 
Similar Threads
final/private method......
Why This Compiler Error?
default constructor access
inheritance
Cannot reference this before supertype constructor?