• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Necessary to initialize bean properties?

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For any java file, is it necessary to initialize the bean properties, for example



or there is no need for that. just this below will do?




can someone clarify my problem for me. thanks alot
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you do not need to initialize your properties but according to the "JavaBeans" guidelines "JavaBeans" must have a no-arg constructor. This is not a compiler requirement but is required by tools/frameworks (e.g. Hibernate, Castor XML, ...) which need to make instances of your classes using reflection.

If you want to init your properties you could use "chained constructor":


which looks nicer and is easier to maintain (you only need to init the properties in one constructor, validation (e.g. checking for null) can be put in one place and so on.


pascal
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bean properties are basically class level private variables.If you do not initialize then it would take the default.I use bean only as data carrier and no logic in that.I leave the variables as it is.
 
Life just hasn't been the same since the volcano erupted and now the air is full of tiny ads.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic