Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple question about JavaBeans

 
Jessid Leon Velez Gutierrez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Jessid Leon Velez Gutierrez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
isn't the Java class will have a default no-args constructors? so do you need to explicitly define this no-arg constructor´╝č
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic