Java provides several access modifiers to control the visibility of class members (variables and methods).
- The public modifier makes the member accessible from any other class.
- The protected modifier allows access within the same package and also in subclasses.
- The default (no modifier) gives access within the same package only.
- The private modifier restricts access to within the class itself.
When we want to encapsulate data and restrict it from being accessed or modified outside the class, we use the private modifier.
This is an essential principle of object-oriented programming known as data hiding.
Thus, private is the correct modifier for keeping members strictly within the class.