good practive to have write-once setters?

E

Elhanan

hi..

i have a few java beans which at first didn't have no-ops consturtctors
(as they were sopoused to be created by spring), but i need also to
create them by code, so i thought about using factories. for this i'll
need to have them no-op constuctors

however since a few of these properties are to be used in map hashcodes
and equels implementaitons, i had check in setters if they are allready
been set before, and if so throw an excption..

is this a good thing?
 
M

Manish Pandit

The best way (again, based on the limited context of your situation) to
have a class with accessors and no mutators will be to initialize the
arguments with the constructor. Your constructor can accept the
arguments and initialize them, and the class will expose no mutators,
making it read only.

-cheers,
Manish
 
E

Elhanan

becouse the factory calls an interface typeIdentier, to help it decide
which class to inititize, the class implementing the typeIdentier sends
back the actuall class itself, which i then use newInstance to create a
new instance of it (i don't have newInstance with paramters).

for examplle

i have PosSource,and StandardSource, both share the same interface
source,

the typeIdentifier looks sorta like this:

case ..

return PosSource.class
case
return StandardSource.class
 
L

Lew

Manish said:
The best way (again, based on the limited context of your situation) to
have a class with accessors and no mutators will be to initialize the
arguments with the constructor. Your constructor can accept the
arguments and initialize them, and the class will expose no mutators,
making it read only.

-cheers,
Manish

And declare the members "final" so that they cannot be reset.

This is a step toward making the class immutable, and immutable is often a
Good Thing.

- Lew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top