How to define getter and setter in Java
What Will I Learn?
- How to define getter and setter methods
Difficulty
- Intermediate
Tutorial Contents

Under normal conditions it is not possible to reach a private element in another class. In Java there is a way to access these private elements. In C #, these are called get-set methods. In Java, these are called getter and setter methods.
With these methods private members can be read or their values can be changed. Getter method to read, setter method to write.
The getter methods do not take parameters because they read. Setter methods take parameters because they assign values to private members.
Now, i'm gonna show how to define getter and setter methods with an example.
public Class GetterSetter
{
private int value; //private variables defined
private String name;
public int getValue () { //getter method
return value;
}
public void setValue(int value) { // setter method
this.value = value;
}
public Streing getName () { //getter method
return Name;
}
public void setName(String name) { /setter method
this.name = name;
}
public static void main(String [] args)
{
Getter Setter n1=new GetterSetter();
n1.setName ("Merry");
Sytem.out.println(n1.getName() );
n1.setValue(5);
System.out.println(n1.getValue() );
}
}
On the screen we'll see is;
Merry
5
- We create an object in the main method.
- We call setName () method on this object and we give Merry as parameter. With this, we assign the parameter Merry to the private variable "name".
- We print this variable on the screen we assigned with n1.getName (). This metod gives us the "name" variable because it has the "return name;" line.
- We then call the setValue () method on this object and assign it a value of 5 as a parameter. So we assign 5 parameters to the private variable "value".
- Finally, we call the n1.getValue () method. And this method returns us the "value" variable because it has the "return value;" line.
We can give "getter" methods the name we want. But starting with the "get" progression will increase the readability of the code. In this way, we could read the values of "private" elements and write over them.
We do not only access "private" elements with getter and setter methods. In the constructor of a class we can also assign values to "private" elements. But we can not read the value. Because the builders do not return value. In addition, another method may be called within the constructors.
Now i'll teach the same example with builders.
public class GetterSetter
{
private int value; // private areas
private String name;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name1){
this.name = name1;
}
public GetterSetter(String name1, int number) // constructive
{
setName(name1); // setter method called
setValue(number); // setter method called
}
public static void main(String [] args)
{
GetterSetter n1=new GetterSetter("Susan",10);
System.out.println(n1.getName ());
System.out.println(n1.getValue ());
}
}
On the screen we'll see is;
Susan
10
While we were building the object on the first line in the "main" method, we sent Susan and 10 values to our constructor. In the constructor these values are sent to "setter" methods. So instead of calling "setter" methods in "main", we could assign the values directly to "private" variables. The line below is for us to use for this;
name=name1;
value=number;
Posted on Utopian.io - Rewarding Open Source Contributors
Your contribution cannot be approved because it does not follow the Utopian Rules.
Explanation
You can contact us on Discord.
[utopian-moderator]