Access modifiers are one of the first tools Java gives you to control who can see and change the internals of your classes. By annotating every field, method, or constructor with a visibility keyword, you communicate intent — and let the compiler enforce it. Getting this right from the start is the difference between a class that’s easy to maintain and one that becomes a source of subtle bugs as a codebase grows.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Jhaymayleth/unidad2_java/llms.txt
Use this file to discover all available pages before exploring further.
What Are Access Modifiers?
Every member of a class (field, method, constructor) has a visibility level. Java provides four options, ranging from completely open to completely private. When you omit a modifier, Java uses package-private (the default), which sits betweenprivate and protected in terms of openness.
| Modifier | Same class | Same package | Subclass | Other packages |
|---|---|---|---|---|
public | ✅ | ✅ | ✅ | ✅ |
protected | ✅ | ✅ | ✅ | ❌ |
| (package-private) | ✅ | ✅ | ❌ | ❌ |
private | ✅ | ❌ | ❌ | ❌ |
public — Accessible from Anywhere
A public member is visible to all code in the entire program, regardless of package or class hierarchy. Use it for the API that other classes are explicitly meant to call — constructors, service methods, and fields that are genuinely meant to be shared.
Empleado class from Taller 7, nombre is declared public to allow direct read/write access from outside the class:
nombre is public, calling code can read or write it directly:
private — Accessible Only Within the Declaring Class
A private member is the most restrictive level. Only code inside the same class file can reach it. Any attempt from outside — even a subclass — causes a compile-time error.
salario private forces all changes to go through setSalario(), where the positive-value check lives. Without private, nothing stops a caller from writing emp.salario = -9999; and silently corrupting the object’s state.
protected — Accessible Within the Package and Subclasses
protected is the right choice when a field or method belongs to an inheritance hierarchy and subclasses need direct access, but outside code should stay out. The Vehiculo superclass in Taller 8 uses protected for marca and velocidadMaxima so that the Coche subclass can read them in its own methods:
Coche can reference marca and velocidadMaxima directly because they are protected. Code in a completely unrelated class, however, cannot touch those fields.
Rule of Thumb: Default to private
When in doubt, start with private. Make only the minimum surface area public. This principle — sometimes called least privilege — keeps your classes easy to change later: if salario is private, you can add logging, rounding, or currency conversion inside setSalario() without touching any caller.
Setter validation is not optional polish — it is the entire reason for making a field
private in the first place. A setter without validation is just extra typing. Always add a guard clause (like if (nuevoSalario > 0)) to enforce the invariants your class must maintain.