aspose file tools*
The moose likes Servlets and the fly likes Bean doesn't need constructor? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Bean doesn Watch "Bean doesn New topic
Author

Bean doesn't need constructor?

SoonAnn Lim
Ranch Hand

Joined: Jun 21, 2001
Posts: 155
This question troubles me long time ago and i did not bother to find it out until recently. When creating a bean for jsp, my bean will work perfectly well if i don't include a constructor explicitly. If i provide a constructor for the bean, errors occur at <jsp:useBean .....>. Can somebody give me some explanation? Thanks a lot.
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209

i'm surprised, a bean to work definitely needs a zero argument constructor (which ofcourse if provided if not specified). If you have a single / multiple arg constructor and if u have forgotten to define the zero argument constructor
then the java class is not a bean. So it w'd'nt work.
karthik.

Originally posted by SoonAnn Lim:
This question troubles me long time ago and i did not bother to find it out until recently. When creating a bean for jsp, my bean will work perfectly well if i don't include a constructor explicitly. If i provide a constructor for the bean, errors occur at <jsp:useBean .....>. Can somebody give me some explanation? Thanks a lot.

SoonAnn Lim
Ranch Hand

Joined: Jun 21, 2001
Posts: 155
In my case, if i do not supply a zero argument constructor, the bean works fine. Once i give any explicit constructor, errors occur. It seems like my bean allow only implicit constructor. That is, when a constructor is not specified in the code, a zero argument constructor is generated automatically by the compiler. Correct me if i am wrong.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Well, thats the rule. If you don't supply any constructor AND the super-class of your class has a no-args constructor, Java will supply a default no-args constructor.
IF on the other hand, you supply a with-args constructor, Java assumes you know what you are doing and does not provide a default no-args constructor.
Bill

------------------
author of:
SoonAnn Lim
Ranch Hand

Joined: Jun 21, 2001
Posts: 155
Hi William,
That is the odd thing i want to make it clear. I understand that default constructor will be supplied by Java supper class. However, i am confused when i supply a constructor (either with or w/o arguments) to my bean class, errors are generated. If i take off my constructor and used the default constructor from super class, everything behaves as it should. The key note is <b>Why errors are generated when i supply my constructor for my bean class?</b> Thanks for any comment.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
That's kind of odd, that you supplied a no-arg constructor, and you called super() from within your constructor, as expected by the bean class, and you still get an error.
The only thing I can think of is that, the bean class just does not expect any constructor (with/without args) to be supplied at all. So supplying one does not make it a bean.

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Lets see the exact text of the error, and include the exact text of your jsp:useBean tag. Also the exact text of the constructor that causes the problem.
(Incidently, if your Bean code is not in a package you can get weird results from JSP.)
Bill
SoonAnn Lim
Ranch Hand

Joined: Jun 21, 2001
Posts: 155
I wish i have my compute with me right now. I am not able to test the code until next week. However, if one of you is able to do me a favor, can you add a constructor to NumGuessBean.java which comes with Tomcat distriubtion in "examples" application. In the no argument constructor, can you make up one property and give a default value? I just think of this approach when i reply this post. One note, when i look at Marty's book, most of the bean used in JSP do not contain an explicit constructor, just bunch of setXXX and getXXX methods. The same style can be observed in many javaworld's example. Am I code it wrong in my class?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Bean doesn't need constructor?