wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JavaBean Standards and default constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "JavaBean Standards and default constructor" Watch "JavaBean Standards and default constructor" New topic
Author

JavaBean Standards and default constructor

Kapil Sakhuja
Ranch Hand

Joined: May 09, 2001
Posts: 34
Is default constructor one of the requirements in JavaBean standards/conventions? Wikipedia link for JavaBean does say so clearly but i could not find the same in both SCJP6 Kathy's and Khalid Mughal's book. Was wondering if this is a common errata for both Kathy and Khalid's book OR is there a reason for not explicitly mentioning it with JavaBean naming conventions?
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

first wikipedia is not the source of authenticated information.
2nd in java every class has default constructor implicitly.

hth


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

M M kaimkhani wrote:2nd in java every class has default constructor implicitly.

Not every class has a default constructor. A default constructor is provided by the compiler if our class doesn't has any constructor. If we explicitly define a constructor in our class, then a default constructor is not created by the compiler. So if our class has only one constructor which takes arguments, then our class doesn't has any default constructor (i.e. the no-arg constructor). To be Java Bean compliant, a class must have a public no-arg constructor...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

Ankit Garg wrote:
M M kaimkhani wrote:2nd in java every class has default constructor implicitly.

Not every class has a default constructor. A default constructor is provided by the compiler if our class doesn't has any constructor. If we explicitly define a constructor in our class, then a default constructor is not created by the compiler. So if our class has only one constructor which takes arguments, then our class doesn't has any default constructor (i.e. the no-arg constructor).

oh yeah that`s what i want to say...

Ankit Garg wrote:To be Java Bean compliant, a class must have a public no-arg constructor...

do you mean standard java bean class must have explicitly 0 arg public constructor? I dont think so...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

do you mean standard java bean class must have explicitly 0 arg public constructor? I dont think so...

If we don't declare any constructor, then its fine, explicitly declaring a constructor is not necessary...
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

yeah that`s true.
Kapil Sakhuja
Ranch Hand

Joined: May 09, 2001
Posts: 34
Thanks for your answers guys, but if one of you or somebody could provide a reference/link to some authentic website or book for the same, i would be grateful!

Ankit, i know that a default constructor is implicitly provided if no other constructor is provided, but if a default constructor is required for a JavaBean, shouldn't it be specified explicitly under JavaBean standards. If your answer is yes, isn't this an errata in both Kathy and Khalid's book?

Yes M M, i know wikipedia is not an authentic source, but i wasn't able to find anything more reliable and so i quoted the same. Also, it took me some time to figure out what 'hth' means, so i would really appreciate if you could please write complete words in your answers. Kindly see Javaranch FAQ for more on the same.
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

oh, I thought if you are able to search java bean specification on wikipedia then you must be able to google HTH as well
Anyways, HTH means "hope this helps" and thanks for the JavaRanchFaq.
Kapil Sakhuja
Ranch Hand

Joined: May 09, 2001
Posts: 34
I still haven't got the answer to my question. Could you please put a link to some resource(book/website) which explicitly says that default constructor is/is not required for a JavaBean?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

I still haven't got the answer to my question.

A jave bean requires a public no-arg constructor. If we don't provide an explicit constructor, then the default constructor created by the compiler is fine. If we do provide constructor in our class, we should provide a public no-arg constructor too...
Kapil Sakhuja
Ranch Hand

Joined: May 09, 2001
Posts: 34
Thanks Ankit! But this isn't mentioned in both Kathy's or Khalid's SCJP6 book. errata?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JavaBean Standards and default constructor
 
Similar Threads
JSF Default Constructor in Backing Bean
problem with Getter Method
Simple EL code is not working
Difference between Normal Java Class and Java Bean
class file is not loading while running JSP file