0Pricing
Java Academy · Lesson

Encapsulation (getters/setters)

Learn how to protect fields using private access, and provide getters and setters for controlled access.

Encapsulation (getters/setters) is a free Java Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Encapsulation

Encapsulation: keeping fields private and controlling access through methods. This hides details and protects state.

Why hide fields?

Hiding fields prevents other code from setting invalid values. You decide what changes are allowed.

Getters

Getter: a method that returns the value of a private field.

Setters

Setter: a method that sets the value of a private field. You can add checks inside to block invalid input.

Benefits

Benefits: safer code, easier debugging, and freedom to change the class internals later without breaking other code.

Demo: encapsulation

Code: Dog has private fields. Getters return values; setter checks validity. Invalid age is reset to 0.

public class Main {
  // Class with private fields, getters, and setters
  static class Dog {
    private String name;
    private int age;

    Dog(String n, int a) {
      name = n;
      setAge(a); // use setter for validation
    }

    public String getName() {
      return name;
    }

    public int getAge() {
      return age;
    }

    public void setAge(int a) {
      if (a >= 0) {
        age = a;
      } else {
        age = 0; // default if invalid
      }
    }
  }

  public static void main(String[] args) {
    Dog d = new Dog("Buddy", 3);
    System.out.println(d.getName() + " is " + d.getAge() + " years old.");

    d.setAge(-5); // invalid, will reset to 0
    System.out.println("After invalid set, age = " + d.getAge());
  }
}

Encapsulation check

Quick check: Why do we make fields private and use getters/setters?

Recap

Recap: Encapsulation = private fields + getters/setters. This hides details and keeps objects safe.

Frequently asked questions

Is the “Encapsulation (getters/setters)” lesson free?

Yes — the full text of “Encapsulation (getters/setters)” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Encapsulation (getters/setters)”?

Learn how to protect fields using private access, and provide getters and setters for controlled access. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Encapsulation (getters/setters)” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Classes & Objects
  2. Fields, Methods, Constructors
  3. Encapsulation (getters/setters)
← Back to Java Academy