Getters:
Methods that retrieve (or "get") the value of a private field.
Typically prefixed with get followed by the field name in camel case.
Setters:
Methods that update (or "set") the value of a private field.
Usually prefixed with set followed by the field name in camel case.
class Person {
private String name; // Private field
// Getter method
public String getName() {
return name;
}
// Setter method
public void setName(String name) {
this.name = name;
}
}
public class Main {
public static void main(String[] args) {
Person p = new Person();
p.setName("Alice"); // Setting the name
System.out.println(p.getName()); // Getting the name
}
}