Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the need for getters and setters ?

 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you make member variables private, you might need to provide getters and setters. Are there any other reasons why they are needed ?
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Data encapsulation is essential in any program.
 
E. Perry
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it can save you a lot of LOC.
consider writing every method in the superclass using the get and set method instead of directly accessing the Fields
This way, in every subclass of such a class, you only need to rewrite the get and set methods [consider: ( return super.getvalue();) etc.. ].
the rest will work as is, because they do not attempt t odirectly access the data, but rather to call the superclass functions instead.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Jack wrote:If you make member variables private, you might need to provide getters and setters. Are there any other reasons why they are needed ?

1. For access. If fields are private, nothing outside the class will be able to see them.
2. For verification. A setter can ensure that the value being set is correct.
3. Because the object is a Bean.
4. For ease of use (eg, display). It may make sense to have a getter return a field value in something other than its native form (eg, as a String).
5. To provide a 'view' of a piece of data, eg:and there's probably many more I've missed.

That said, you should not just provide getters and setters willy-nilly - especially setters. Only create them if you know that something outside the class is going to need to use them.

Winston
 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
meeta gaur wrote:Data encapsulation is essential in any program.


why is it essential is what my question is all about.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the Wikipedia article: Data encapsulation. It doesn't say much except to point to two other articles, about "information hiding" and "separation of concerns". You should read them both.
 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Andy Jack wrote:If you make member variables private, you might need to provide getters and setters. Are there any other reasons why they are needed ?

1. For access. If fields are private, nothing outside the class will be able to see them.
2. For verification. A setter can ensure that the value being set is correct.
3. Because the object is a Bean.
4. For ease of use (eg, display). It may make sense to have a getter return a field value in something other than its native form (eg, as a String).
5. To provide a 'view' of a piece of data, eg:and there's probably many more I've missed.

That said, you should not just provide getters and setters willy-nilly - especially setters. Only create them if you know that something outside the class is going to need to use them.

Winston


Thanks.

1 - I make member variables private and put gets and sets without really understanding why i am doing all that. What is the problem in keeping every member variable public ?
2 - Yes, good point. If we don't do the verification, any code can assign any values (legal or illegal) to the member variables.
3 - Heard of beans, but don't know why beans need gets n sets.
4 - you mean something like this - a member int returned as a String ?
5 - I am not sure what that means.

I am also not sure when we must use getters and setters and when we should not.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Jack wrote:
Winston Gutkowski wrote:1. For access. If fields are private, nothing outside the class will be able to see them.
2. For verification. A setter can ensure that the value being set is correct.
3. Because the object is a Bean.
4. For ease of use (eg, display). It may make sense to have a getter return a field value in something other than its native form (eg, as a String).
5. To provide a 'view' of a piece of data,

1 - I make member variables private and put gets and sets without really understanding why i am doing all that. What is the problem in keeping every member variable public ?

If you keep your members public then you don't have control over #2, #4. You also expose implementation details. If you don't hide the data type of a variable, say an int, then later when you need to use a BigInteger then any code which used the variable directly (because you allowed them to) will break. So rule #1, all variables are private. A side effect, if you need access from outside the class you need to provide get/set methods.
2 - Yes, good point. If we don't do the verification, any code can assign any values (legal or illegal) to the member variables.
3 - Heard of beans, but don't know why beans need gets n sets.

Beans are simply data holders where the fields are accessed via get and set methods. If you name things correctly then your Bean can be used in specific contexts which can take advantage of them, like JSP JSTL, or some GUI builders.
4 - you mean something like this - a member int returned as a String ?

Yes, and you will be able to format the String your way in the getter.
5 - I am not sure what that means.

Notice in his code example he is using a get method to access a value which doesn't actually exist - the getNetPrice() provides a view into the data that is a composite of the two other data members.

I am also not sure when we must use getters and setters and when we should not.

Start with the assumption that you don't, you have a bunch of data, it is all private, and no one ever needs to access it. Then if you find that some other class needs to see the value of some private variable, add a getter. And if you can make a really good case that some external class needs to be able to change the value of the variable then add a setter.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic