| Author |
why use getter setters in beans
|
anagha patankar
Ranch Hand
Joined: Dec 26, 2005
Posts: 53
|
|
hello , We have been using Value objects which we use to get data from db . these Value objects are taken all the way to the Ui for displaying . Almost all cases : we have getters and setters ( public ) for private attributes . My question is : why do we need getters and setters at all ? to answer my own question : for Some attributes it should not be possible to change the value so we set only getters and not setters . but then again - the counter arguement being : To call the getter - I need an instance of the Value Object If I have an instance of the Value Object - then I can anyways make a change to its attributes . Please let me know your thoughts . Thanks , -anagha
|
 |
sven studde
Ranch Hand
Joined: Sep 26, 2006
Posts: 148
|
|
To call the getter - I need an instance of the Value Object If I have an instance of the Value Object - then I can anyways make a change to its attributes . Please let me know your thoughts .
My thoughts are that I don't understand your statement. If you have an instance of an object, why can you necessarily make changes to its attributes? See this example:
---------- javac ---------- AATest1.java:15: num has private access in ValueObject vObj.num = 30; ^ 1 error Output completed (17 sec consumed) - Normal Termination
[ October 04, 2006: Message edited by: sven studde ]
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
I assume that by Value Object you actually mean a Data Transfer Object. I agree that Data Transfer Objects don't need getters and setters, public fields are just well. In fact calling them "objects" is kind of a stretch - they are pure data structures, no behaviour at all. Then again, if you are not writing a distributed system, you probably shouldn't get DTOs from your database layer, but fully fledged domain objects.
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
 |
|
|
subject: why use getter setters in beans
|
|
|