0
0
Javaprogramming~5 mins

Encapsulation best practices in Java

Choose your learning style9 modes available
Introduction

Encapsulation helps keep data safe and organized inside a class. It hides details so others use the class easily without mistakes.

When you want to protect important data from being changed directly.
When you want to control how data is accessed or updated.
When you want to make your code easier to understand and maintain.
When you want to hide complex details and show only simple actions.
When you want to prevent accidental errors by limiting direct access.
Syntax
Java
public class ClassName {
    private DataType variableName;

    public DataType getVariableName() {
        return variableName;
    }

    public void setVariableName(DataType variableName) {
        this.variableName = variableName;
    }
}

Use private to hide variables inside the class.

Use public getter and setter methods to control access.

Examples
This example hides the name variable and allows safe access through methods.
Java
public class Person {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
This example controls how money is added or taken out, protecting the balance from wrong changes.
Java
public class BankAccount {
    private double balance;

    public double getBalance() {
        return balance;
    }

    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }

    public void withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance -= amount;
        }
    }
}
Sample Program

This program shows a car with a model and speed. Speed cannot be set to a negative number because of the check inside the setter method.

Java
public class Car {
    private String model;
    private int speed;

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        if (speed >= 0) {
            this.speed = speed;
        }
    }

    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.setModel("Toyota");
        myCar.setSpeed(50);
        System.out.println("Model: " + myCar.getModel());
        System.out.println("Speed: " + myCar.getSpeed());

        myCar.setSpeed(-10); // This will not change speed because of check
        System.out.println("Speed after invalid update: " + myCar.getSpeed());
    }
}
OutputSuccess
Important Notes

Always keep variables private to protect data.

Use getter and setter methods to add checks or rules when accessing data.

Do not expose variables directly to avoid accidental changes.

Summary

Encapsulation hides data inside classes using private variables.

Use getter and setter methods to control how data is accessed or changed.

This helps keep your code safe, clean, and easy to fix or improve later.