why use getter setters in beans
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 19 years ago
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:
[ October 04, 2006: Message edited by: sven studde ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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
| Power corrupts. Absolute power xxxxxxxxxxxxxxxx is kinda neat. The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |










