Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Simple question about JavaBeans

 
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.
 
Rancher
Posts: 43016
76
  • 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!
 
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?
 
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´╝č
 
Rancher
Posts: 377
Android Spring Java
  • 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
 
    Bookmark Topic Watch Topic
  • New Topic