Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor

 
Vineela Devi
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
A very basic doubt.
Is the no argument constructor same as default constructor?
Vineela
 
Sanjay Gandham
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite. As per what I understood, only the compiler provided no-arg constructor is referred to as default constructor.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vineela and Sanjay,
Either way no-arg constructor is the default constructor.
Regards
Maulin
 
Sanjay Gandham
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
rong zh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Corey,
Thanks for the clarification.
I overlooked the "linguistic/semantic" part in the question
Thanks
Maulin
 
Vineela Devi
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Corey,

Thanks a lot for ur explanation.
Vineela
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic