• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Simple question about JavaBeans

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 35
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic