my dog learned polymorphism*
The moose likes Java in General and the fly likes why use getter setters in beans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "why use getter setters in beans" Watch "why use getter setters in beans" New topic
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why use getter setters in beans
 
Similar Threads
getters/setters are evil?
IBM Test: Coupling question
getter and setter generation with annotations
How to handle huge number of fields in Data Beans?
Is including Mutable Objects in VO/DTO pattern a violation of OO Programming ?