Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaBean Standards and default constructor

 
Kapil Sakhuja
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first wikipedia is not the source of authenticated information.
2nd in java every class has default constructor implicitly.

hth
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah that`s true.
 
Kapil Sakhuja
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit! But this isn't mentioned in both Kathy's or Khalid's SCJP6 book. errata?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic