Java Getters & Setters
In this page:
What are Getters and Setters?
Getters and setters are public methods that provide controlled read/write access to a class's private fields, letting the class keep its internal data hidden while still allowing outside code to interact with it safely.
Example: What are Getters and Setters?
class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Main {
public static void main(String[] args) {
Person p = new Person();
p.setName("Alice");
System.out.println(p.getName());
}
}
Login to try C/C++/Java/PHP code in the editor
Adding Validation in Setters
Because a setter is regular code, not a direct field assignment, it can validate incoming values before storing them — rejecting a negative age or an empty name, for example — which a bare public field could never do.
Example: Adding Validation in Setters
class Person {
private int age;
public void setAge(int age) {
if (age < 0) {
System.out.println("Invalid age");
return;
}
this.age = age;
}
public int getAge() {
return age;
}
}
public class Main {
public static void main(String[] args) {
Person p = new Person();
p.setAge(-5);
p.setAge(25);
System.out.println(p.getAge());
}
}
Login to try C/C++/Java/PHP code in the editor
Read-Only Classes
Omitting setters and providing only getters makes a class effectively read-only after construction: callers can inspect its state but never change it, which is a useful way to build simple immutable-style value objects.
Example: Read-Only Classes
class Point {
private final int x;
Point(int x) {
this.x = x;
}
public int getX() {
return x; // no setter: read-only after construction
}
}
public class Main {
public static void main(String[] args) {
Point p = new Point(5);
System.out.println(p.getX());
}
}
Login to try C/C++/Java/PHP code in the editor
Write-Only Classes
The reverse pattern — setters with no getters — creates a write-only class, useful for holding sensitive values like a password where you want code to be able to set the value but never read it back out.
Example: Write-Only Classes
class Login {
private String password;
public void setPassword(String password) {
this.password = password; // no getter: write-only
}
}
public class Main {
public static void main(String[] args) {
Login login = new Login();
login.setPassword("secret");
System.out.println("Password set");
}
}
Login to try C/C++/Java/PHP code in the editor
Using the 'this' Keyword
Inside a setter, the parameter is often named the same as the field it updates, so this distinguishes the parameter (name) from the object's own field (this.name) — without it, name = name; would just reassign the parameter to itself and never touch the field.
Example: Using the 'this' Keyword
class Person {
private String name;
public void setName(String name) {
this.name = name; // this.name is the field, name is the parameter
}
}
public class Main {
public static void main(String[] args) {
Person p = new Person();
p.setName("Bob");
System.out.println("Name set");
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: