• 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

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.
 
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic