← Back to Core Java Course | Chapter 7: OOP Core | Lesson 7 of 11

Java Getters & Setters

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?

java
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());
	}
}

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

java
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());
	}
}

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

java
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());
	}
}

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

java
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");
	}
}

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

java
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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.