aspose file tools*
The moose likes Beginning Java and the fly likes What is the need for getters and setters ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the need for getters and setters ?" Watch "What is the need for getters and setters ?" New topic
Author

What is the need for getters and setters ?

Andy Jack
Ranch Hand

Joined: Nov 22, 2012
Posts: 257
If you make member variables private, you might need to provide getters and setters. Are there any other reasons why they are needed ?

Java Newbie with 72% in OCJP/SCJP - Super Confused Jobless Programmer.
I am a "newbie" too. Please verify my answers before you accept them.
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Data encapsulation is essential in any program.


My one regret in life is that I am not someone else.
- Woody Allen
E. Perry
Greenhorn

Joined: Jan 19, 2013
Posts: 5
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

Joined: Mar 17, 2011
Posts: 8158
    
  23

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Andy Jack
Ranch Hand

Joined: Nov 22, 2012
Posts: 257
meeta gaur wrote:Data encapsulation is essential in any program.


why is it essential is what my question is all about.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18856
    
    8

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

Joined: Nov 22, 2012
Posts: 257
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the need for getters and setters ?