This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Reset property and resuse bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reset property and resuse bean" Watch "Reset property and resuse bean" New topic

Reset property and resuse bean

Nitin Gaur

Joined: Sep 06, 2003
Posts: 27
Say its a simple java bean with some members and getter/setters. I create an instance of bean and call setters to set values. Now I want another copy of bean but with just one different property value. So the question is -
- Do I have to again instantiate the bean and call all setters again?
- Just call the setter for one property again and reuse the existing instance? Is this right way to do it?

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44616
If you want two instances you will need two instances. If you have one instance and reset one field, then you will lose the information about what that field was before.

How about giving your bean class a copy constructor? Then you can copy all the fields, and reset one only. Or even a clone() method call and a single field reset.
Beware: if any of those fields is a mutable reference type, then alteration to its state in one bean may be reflected in other beans with the same object in that field.
I agree. Here's the link:
subject: Reset property and resuse bean
jQuery in Action, 3rd edition