• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 756
  • Last Modified:

Why Getter and Setter methods?

Could you please tell me why we use Getter and Setter methods in Java ?
0
jaggybala2
Asked:
jaggybala2
  • 4
  • 3
  • 2
  • +1
1 Solution
 
zzynxSoftware engineerCommented:
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
 
girionisCommented:
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
 
sciuriwareCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sciuriwareCommented:
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
 
girionisCommented:
0
 
jaggybala2Author Commented:
now, i am much confused of whom to give the points. O:-)
0
 
girionisCommented:
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
 
jaggybala2Author Commented:
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
 
girionisCommented:
Not at all, glad you are helped :)
0
 
sciuriwareCommented:
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

Independent Software Vendors: 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!

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now