Example Application: Getters and Setters
Create a class and declare any variable:
public class Person {
private String name;
private String familyName;
private int age;
}
Class person declares getters.
public String getName() {
return name;
}
public String getFamilyName() {
return familyName;
}
public int getAge() {
return age;
}
It is possible to create a custom getter.
public String getFullName() {
return name + " " + familyName;
}
Class person declares setters.
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setFamilyName(String familyName) {
this.familyName = familyName;
}