Solved

Why Getter and Setter methods?

Posted on 2004-08-05
10
715 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

832 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