Encapsulation in Java means bundling data and the methods that work on it inside one class, and hiding the data from outside code. You make fields private and expose controlled access through public getters and setters, so the class can validate input and change its internals later without breaking callers.
This guide shows how to encapsulate a class step by step, what getters and setters really buy you, and how modern Java (records, immutability) changes the picture. Examples run on Java 21 and Java 25 LTS.
Encapsulation has two parts: keeping state private, and offering a small public interface to read or change it. The class owns its data, and other classes ask it to do things instead of reaching in.
Without encapsulation any code can break an object:
Nothing prevents a negative balance, and if you later need to log every change or store the value in cents, every caller must be edited. Encapsulation removes that problem.
The standard pattern is a private field, a public getter to read it and, only when change is allowed, a setter that checks the new value.
The constructor calls the setters so validation lives in one place. The naming convention is getX, setX, and isX for boolean fields; frameworks such as Jackson and Spring rely on it.
age as a birth date later and keep getAge() working.A getter and setter for every field is not automatically good design. If a setter simply assigns the value with no rules, you have only moved the problem. Prefer methods that express intent, such as deposit(amount) and withdraw(amount), over setBalance(x).
Encapsulation can leak when a getter returns a mutable object. The caller then changes your private data without going through any method.
Returning members directly would let any caller call clear() on it. An unmodifiable view, or a copy with List.copyOf(members), keeps the list protected. The same applies to arrays and to Date objects, and to values you accept in constructors: copy them if the caller could still change them.
The strongest encapsulation is an object that cannot change after creation. Mark fields final, provide no setters and copy mutable inputs. Since Java 16 a record does most of this for you.
A record generates private final fields, accessor methods named after the components (x(), not getX()), plus equals, hashCode and toString. Use records for plain data carriers and classic encapsulated classes when the object has changing state and behaviour.
It is hiding an object's data behind methods. Outside code can use the object only through the public methods you choose to expose.
They are closely related. Data hiding means making fields private, and encapsulation is the wider idea of grouping data and behaviour while exposing a controlled interface.
No. Add a getter only when callers need to read the value and a setter only when they may change it. Fields that never change should be final with no setter.
For immutable data, yes. Records give you accessors and no setters. Classes with changing state still use setters or intent-revealing methods.
Encapsulation keeps fields private and exposes validated, minimal access through methods. It protects invariants, lets you change internals safely and makes classes easier to maintain. Watch out for getters that return mutable objects, and prefer immutability or records when the data does not need to change.