Java Encapsulation
Java encapsulation means keeping an object’s data inside its class and controlling access to it through methods. It helps protect fields from invalid changes and makes a class easier to use correctly.
In practice, encapsulation usually means making fields private and providing public methods when other code should read or update those fields.
Why Encapsulation Matters
If every field is public, any part of the program can change an object’s data directly. That can make bugs hard to find because the class has no chance to check whether a new value is valid.
Encapsulation gives the class control over its own state. Other code asks the object to do something, and the object decides how to handle the request.
Private Fields And Public Methods
The next example stores a student’s name and score in private fields. Code outside the class cannot access those fields directly, so it uses methods instead.
class Student {
private String name;
private int score;
Student(String name, int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student("Maya", 85);
System.out.println(student.getName());
System.out.println(student.getScore());
student.setScore(92);
System.out.println(student.getScore());
}
}
Output:
Maya
85
92
The fields name and score are private, so main cannot use student.name or student.score. Instead, main calls getName, getScore, and setScore.
Getters And Setters
A method that returns a private field is often called a getter. A method that changes a private field is often called a setter.
getName()returns the current value ofname.getScore()returns the current value ofscore.setScore(int score)updates the value ofscore.
You do not need to write a setter for every field. If a value should not change after the object is created, you can provide only a getter.
Adding Validation
Setters become more useful when they check values before storing them. In this example, the score can only be changed if it is between 0 and 100.
class GradeBook {
private int score;
GradeBook(int score) {
setScore(score);
}
public int getScore() {
return score;
}
public void setScore(int score) {
if (score >= 0 && score <= 100) {
this.score = score;
}
}
public String getLetterGrade() {
if (score >= 90) {
return "A";
} else if (score >= 80) {
return "B";
} else if (score >= 70) {
return "C";
} else {
return "Needs practice";
}
}
}
public class Main {
public static void main(String[] args) {
GradeBook grade = new GradeBook(88);
System.out.println(grade.getScore());
System.out.println(grade.getLetterGrade());
grade.setScore(105);
System.out.println(grade.getScore());
grade.setScore(95);
System.out.println(grade.getScore());
System.out.println(grade.getLetterGrade());
}
}
Output:
88
B
88
95
A
The attempted value 105 is ignored because it is outside the allowed range. The object keeps its previous valid score of 88. Later, 95 is accepted and the letter grade changes to A.
Encapsulation And Class Design
Encapsulation is not only about hiding fields. It is about giving a class a clear public interface. Other code should not need to know every detail of how the class stores its data.
For example, getLetterGrade() returns useful information without exposing the grade calculation to main. If the grading rules change later, the change can stay inside the GradeBook class.
Common Mistakes
- Making fields
publicwhen they should be protected by methods. - Writing setters that accept invalid values without checking them.
- Adding setters for fields that should never change after construction.
- Thinking private fields are unusable. They are usable inside the same class.
Takeaway: encapsulation keeps fields private and exposes controlled methods so objects can protect their own data.
