What are getter methods and setter methods in Java? What is their technical term in Java?
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:.
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.
Example
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.
Cheers,
jrh
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.