Anatomy of a Class · Anatomia de uma Classe
| English | Português |
|---|---|
| encapsulation/ɪnˌkæpsjʊˈleɪʃn/ | encapsulamento |
| instance variables/ˈɪnstəns ˈveərɪəblz/ | variáveis de instância |
Private fields still need checked methods
- A Student has a private score, but
public void setScore(int s) { score = s; }still lets a caller supply -5. Private access alone does not enforce a non-negative score. - A guarded setter can reject that update. Check the constructor and every other method that writes the field too; encapsulation makes a validation policy easier to enforce but does not create it automatically.
Making score private automatically rejects negative arguments supplied to an unguarded public setter.
False: the setter is class code and can assign the negative value. Validation must be implemented.
The three parts of a class
- The class designs here contain instance fields for data, constructors for initial setup, and methods for behavior. These are three useful parts to identify; formally, Java does not classify constructors as class members.
- Trace a Student from construction to an update: the constructor initializes its score field, and an instance method later reads or changes that object's score. A static method instead has no implicit current object.
Instance variables hold state
- Instance variables 实例变量 store each object's own state · estado.
private String name; private int score;— declared inside the class, outside any method.- Every object gets its own copy — one student's
scoreis separate from another's. - These persist for the life of the object (unlike local variables).
private and public
- Access modifiers control who can reach a member.
private— usable only inside the class;public— exposed to other code that can access the class.- Instance variables are usually
private; methods that form the interface arepublic. - This hiding of internal data is called encapsulation 封装.
Which three parts organize the Student class design taught here?
Instance fields hold state, constructors initialize new objects, and methods provide behavior. Constructors are not formally class members in Java terminology.
Instance variables are usually declared...
Private data + public methods = encapsulation.
Keeping data private and exposing it only through methods is called ___ (one word).
Encapsulation protects an object's data.
Two objects of the same class share one copy of each instance variable.
Each object has its own copy of the instance variables.
For the top-level Student class shown here, an unrelated external class may directly read a private field...
Private access prevents an unrelated external class from directly reading the field. Code inside Student may read another Student object's private field.
Why keep data private
- Encapsulation · Encapsulamento prevents unrelated external code from directly assigning a private field. Code inside Student can still read or write private fields, including those of another explicitly referenced Student.
- Outsiders interact only through public methods, which can validate and control access.
- You could reject a negative score in a method; a public variable has no such guard.
- Private fields with a deliberately chosen public interface support controlled updates. Each method and constructor must still implement the intended rules; returning a mutable internal object can also expose changes indirectly.
Keep instance variables private; expose behavior through public methods. A public field lets any code set it to anything (a negative score, a null name) with no checks. Encapsulation · Encapsulamento funnels all access through methods that can validate the change. Each object has separate instance-field storage, but reference fields in different objects may point to the same shared object. Distinguish separate fields from separate referenced objects.
Two objects have separate reference fields, so those fields must point to different objects.
False: separate reference fields can both point to the same mutable object.
Two students reference the same initially empty ArrayList. One adds a single item. What list size does the other reference observe?
Both references point to the same list object, whose size is now 1.
Anatomy of a Student class:
- Instance variables:
private String name; private int score; - Constructor:
public Student(String n) { ... } - Methods:
public void addScore(int s) { ... },public int getScore() { ... }
Separate field storage from shared objects
- If two Student objects both store a reference to the same ArrayList, their list fields are separate but the referenced list is shared. Adding an item through either reference changes that one list.
- Reassigning one student's list field to a new ArrayList changes only that field. Follow reference arrows when predicting state; private access and object identity answer different questions.
A class has instance variables (each object's state · estado), constructors (set up new objects), and methods (behaviors). Instance variables are usually private and accessed through public methods — this encapsulation protects the data. Each object holds separate instance fields; the objects referenced by those fields may still be shared.
Each object has its own state · Cada objeto tem seu próprio estado
a.score and b.score are separate — each object owns its state.