Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Default Constructor

 
Patrick Day
Greenhorn
Posts: 8
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wondering if someone could explain this concept to me. "A no-argument Constructor is not necessarily the default Constructor, although the default Constructor is always a no-argument Constructor". This completely confused me, I hope someone can explain this in layman terms.
 
Henry Wong
author
Marshal
Pie
Posts: 21117
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A no-arg constructor is a constructor that takes no arguments. A default constructor is a constructor that the java compiler creates for you when you don't define any constructors in your class definition.

If you define a constructor in your class, then no default constructor will be created for you. And if your constructor takes no arguments, then your constructor (which is not a default constructor) is a no-arg constructor.

Henry
 
Campbell Ritchie
Sheriff
Pie
Posts: 48953
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:You think those two classes do the same thing, and you would be right. And if you compile them, and then inspect the bytecode, like this:... you will see the no-args constructor, with and without its contents. I think it will say something like <init> against it.
Now repeat the procedure with the Baa class. You can see that the compiler has added a constructor, which is a default constructor, doing the absolute minimum possible for a constructor, ie calling the superclass' constructor.
The Java™ Language specification tells you about default constructors, but it isn't easy to read.
 
Patrick Day
Greenhorn
Posts: 8
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both Henry & Campbell, I understand it now
 
Campbell Ritchie
Sheriff
Pie
Posts: 48953
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic