Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Object-Oriented Concepts

Encapsulation in Java

Encapsulation in Java: hide fields behind methods, enforce invariants, avoid leaking mutable state with defensive copies, and choose the right access level.

Encapsulation means keeping an object's internal state private and letting the outside world interact with it only through a small, deliberate set of methods. The goal is not secrecy for its own sake. It is to guarantee that the object can never be put into an invalid state, and to be free to change the inside later without breaking callers.

In Java you encapsulate with access modifiers: make fields private, expose behavior through public methods, and let the class itself check every change. A class that exposes a public mutable field gives up all of its guarantees, because any code anywhere can write any value.

Java
class Temperature {
    private double celsius;                     // hidden
    public void setCelsius(double c) { ... }    // controlled access
}

Try it

Java Enforcing an invariant

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Leaking internal state and defensive copies

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Access levels

Modifier Visible to
private The declaring class (and its nested classes) only.
(none, package-private) Classes in the same package.
protected The same package, plus subclasses anywhere.
public Everyone (and, with modules, only if the package is exported).

Usage notes

  • Default to private and widen only when needed. Narrow access leaves you free to refactor.
  • Getters and setters are not encapsulation by themselves. A setter that accepts anything is just a public field with extra typing. Prefer methods that express behavior (deposit, withdraw) and validate their arguments.
  • Not every field needs a getter. Expose what callers need, not the internal representation.
  • Mutable components leak. Returning or storing a reference to an internal List, array or Date hands out control. Copy on the way in and out, or return unmodifiable views and immutable types.
  • Make as much as possible immutable. final fields and no setters give you thread safety and simple reasoning (immutable classes). Records are a compact way to write them.
  • Validate in constructors so an invalid object can never exist.
  • Packages and modules add coarser boundaries. A package-private class is invisible outside its package; JPMS can hide whole packages.
  • Reflection can bypass private (subject to module rules). Encapsulation protects against mistakes, not against a hostile program in the same JVM.
  • Do not expose protected fields. A subclass can break the parent's invariants as easily as any outside code.