• Post Reply Bookmark Topic Watch Topic
  • New Topic

cannot understand disadvantages of Java Bean Class  RSS feed

 
Kasun Wixkramanayake
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, the JavaBeans pattern has serious disadvantages of its own.
Because construction is split across multiple calls, a JavaBean may be in an
inconsistent state partway through its construction. The class does not have
the option of enforcing consistency merely by checking the validity of the constructor
parameters. Attempting to use an object when it’s in an inconsistent state
may cause failures that are far removed from the code containing the bug, hence
difficult to debug.

i took this from effective java book. Can you help me to understand this pargarph
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you ever compared the Bean pattern with the immutable object pattern?

Imagine you have a Bean with a no‑arguments constructor. If you create an instance in a threaded environment, you may find that instance is used by parallel threads while its fields still point to 0 or null. If you consider an immutable object, you will find that scenario cannot occur. The ConstructorProperties annotation was introduced in Java6 to allow an immutable object to be used as a Bean
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This, of course, does not mean not to use the bean pattern. Beans are very useful, in fact essential, in may environments. Just be aware of how they work and their limitations
 
Kasun Wixkramanayake
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because construction is split across multiple calls, a JavaBean may be in an
inconsistent state partway through its construction. The class does not have
the option of enforcing consistency merely by checking the validity of the constructor
parameters

Actually i couldnot understand the above few lines.can you eloborate your answers
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Notice that it takes three statements to complete the construction of this bean. After they are complete, the bean is consistent. Between the calls to the two setters, it isn't consistent. Does that help?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!