Take the 2-minute tour ×

Possible Duplicate:
What are the uses of getter/setters in Java?

What are getter methods and setter methods in Java? What is their technical term in Java?

share|improve this question
Im sure this has been asked before. –  Zaki Sep 28 '09 at 7:40
not sure searched but didnt find specific answer –  Abhishek Sanghvi Sep 28 '09 at 7:41
This might help. stackoverflow.com/questions/1345753/… –  Zaki Sep 28 '09 at 7:44
Thanks a lot Zaki –  Abhishek Sanghvi Sep 28 '09 at 7:47
add comment

marked as duplicate by kleopatra, Luke Woodward, paradigmatic, krock, Linger Nov 11 '12 at 15:58


3 Answers

Getter and setter methods are methods that are used to manipulate the value of a single "property" of an object.

Usually, the names of these methods are getProperty() and setProperty(PropertyType value), where Property is the name of the property which these methods modify/access.


class Person {
    private String name; // the property "name"
    public String getName(); // getter for the property "name"
    public void setName(String newName); // setter for the property "name"

And, "setter" and "getter" are/have now become universally consistent terms in the programming world. As far as I know, they are the technical terms. If they are not, you can still use them without fear of being misunderstood.


share|improve this answer
The technical term is 'accessor methods', with the problem that people may not know what you're talking about ;-). –  NomeN Sep 28 '09 at 8:33
add comment

Getter and Setter methods are ways to access internal variables of a class externally.

They are much safer than setting the variable to be public as you can guarentee usage and thread-safety.

I found this thread through a google search:.

share|improve this answer
Um no, they are pretty much exactly the same as setting the variable to public. They are a horrible antipattern and I wish borland had never hacked them into the java mindset as a stupid way to get their design tools to work. –  Bill K Sep 28 '09 at 8:13
They are definitely not 'antipatterns', ignoring the wrong use of the term here. Imagine the situation where you need to debug your code, do you want to find all places where the variable is changed or just put a println/breakpoint in the setter? I know what I'd rather do... And then there are all the interface arguments. –  NomeN Sep 28 '09 at 8:29
Getters and setters allow validation logic. Changing a public field to getter and setters is a breaking change, so people usually use getters and setters in stead of public fields in case they need to add validation logic later. –  Joren Sep 28 '09 at 8:59
add comment

A frequently used term for these methods is 'accessor methods', and probably the best rule of thumb is to only use them if they are needed, and if you do use them use only the narrowest visibility required.

share|improve this answer
I have also heard the phrase "mutators" used to describe setters. –  Adamski Sep 28 '09 at 7:56
I'd +1 the "accessor methods" and -1 the "only use them if you need them". So nothing from me, sorry. –  NomeN Sep 28 '09 at 8:31
add comment
