• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Bean and Constructor  RSS feed

 
Maneesh Chauahn
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
As i am doing a project in the java bean, and every where i find that the while writting the java bean a developer must define default constructor for the java bean.

So my question is that java it self provides a default constructor for every class so why in the case of java bean it's mandatory to define a default constructor for the java beans in the code


Thanks in advance for your prompt answer

Maneesh
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First let's get the terminology straight: you cannot defined a default constructor, because by definition only the compiler can provide one. The default constructor is the constructor the compiler provides if you don't provide one.

A Java Bean doesn't need a default constructor, but a no-arg constructor (a constructor that doesn't get any arguments). The default constructor is a no-arg constructor.

You are right, if your bean doesn't need other constructor, the default constructor provided by the compiler is a valid Java Bean constructor.

It's just when you also write other constructors, you will also need to define a no-arg constructor explicitely, because the compiler won't create the default constructor any longer.

Does that sound reasonable?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!