File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Simple question about JavaBeans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Simple question about JavaBeans" Watch "Simple question about JavaBeans" New topic
Author

Simple question about JavaBeans

Jessid Leon Velez Gutierrez
Ranch Hand

Joined: Jan 19, 2011
Posts: 35
Hello. I am studying for SCWCD exam and in one of the questions I found, said that a JavaBean will generate a compile time error just because it does not have non-arg constructor. Explicitly the author of the exam says:

"Since MyBean does not have a no-arg constructor, the container cannot instantiate it and hence a Compilation error
would occur."


MyBean is the class with a no arg-constructor. I think this is wrong, because it would generate a run-time error, when the container tries to instantiate the bean. Am I wrong?

thank you!

The exam i am talking about is Practice Test I, question number 19 from whizlabs.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42595
    
  65
The problem would be detected at runtime of the web app, but at compile time of the JSP (since that's where the code containing the no-arg constructor is used).


Ping & DNS - my free Android networking tools app
Jessid Leon Velez Gutierrez
Ranch Hand

Joined: Jan 19, 2011
Posts: 35
Ulf Dittmer wrote:The problem would be detected at runtime of the web app, but at compile time of the JSP (since that's where the code containing the no-arg constructor is used).


mmm.... all right Ulf. I think I get it now. Thanks a lot!
Marcos Vidolin
Greenhorn

Joined: May 10, 2011
Posts: 28
Yeap
I think you are rigth.
the compile tries to find out the non-arg constructor because it is in the specification.
How would you do to instantiate the Bean using <jsp:useBean> tag for example, without non-arg constructor?
wei liu
Ranch Hand

Joined: Nov 19, 2008
Posts: 35

isn't the Java class will have a default no-args constructors? so do you need to explicitly define this no-arg constructor´╝č


SCJP 1.6 in 2010
SCWCD5 in 2011
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

wei liu wrote:isn't the Java class will have a default no-args constructors? so do you need to explicitly define this no-arg constructor´╝č


Only if you haven't defined any other constructors, if you have then you will need to explicitly create a no-arg construcor as well.

Sean


I love this place!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Simple question about JavaBeans