Solved

Why Getter and Setter methods?

Posted on 2004-08-05
10
730 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

705 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