Solved

Why Getter and Setter methods?

Posted on 2004-08-05
10
720 Views
Last Modified: 2010-03-31
Could you please tell me why we use Getter and Setter methods in Java ?
0
Comment
Question by:jaggybala2
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 11723940
1) It's "cleaner" not to mess with the data members directly.
2) Maintenance

    public int getWhatever() {
        return whatever1;
    }


    If that should ever change in

    public int getWhatever() {
        return whatever1 * whatever2;
    }

    You have to change it only here. Every place where getWhatever() is used can stay as it is.

    That's not the case if you would have used

             instance.whatever1;        ==>   instance.whatever1 * instance.whatever2;
0
 
LVL 35

Expert Comment

by:girionis
ID: 11723969
I mainly use them in two cases

a) when I want to wrap some value around and I do not want to break the existing code
b) when I want to access variables from inner classes and the calling would be "ugly", I just have an accessor that does the job for me.

Apart from that no special reason. Besides if you do not use them you reduce the code of the programme.
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 25 total points
ID: 11724050
What you address is a basic idea of object oriented programming:
a getter and a setter pair control access to and from a field:

the setter checks if what is offered should be used,
the getter checks if you may see the data and then in what format.
Of course this goes with private access to the field and public, package or protected access to the getter and the setter.
;JOOP!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Expert Comment

by:sciuriware
ID: 11724062
P.S.: all encapsulated constructs, like classes, activexes, beans and the sort are subjected
to obligatory getter and setter - access.
As a side-effect, you can construct access to a field at runtime if you know the name of the class and the field
and if the programmer kept to the standard that field xyz has getXyz() and setXyz().
(that's a plug-in!)
;JOOP!
0
 
LVL 35

Expert Comment

by:girionis
ID: 11724129
0
 

Author Comment

by:jaggybala2
ID: 11724237
now, i am much confused of whom to give the points. O:-)
0
 
LVL 35

Expert Comment

by:girionis
ID: 11724248
You should award the points to the comment you value most, i.e. to the comment you think it provided the best answer. If you value all comments the same a good practice is to award the points to the first one who answered your question.

Another thing you can do (if you have points available) is to increase the number of points and do a split between all the participants (minimum of 20 points each).
0
 

Author Comment

by:jaggybala2
ID: 11724252
i go with sciuriware as his explanation is *much* understandable for me. I hope other experts dont get angry. afterall, i have very less points.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11724297
Not at all, glad you are helped :)
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 11724338
jaggybala2, we're not exclusively here for the points; we learn too.
And I mostly appreciate your comment.
Before all I want to improve the quality of my explanations.

;JOOP!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 97
servlet example 11 56
String array comparison 4 45
ejb mdb examples 1 19
In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question