private — hide internal datapublic set/get methodsnewthis(...) calls another constructor in the same class| Modifier | Visible to |
|---|---|
public | everyone |
protected | package + subclasses |
| (none) package | same package only |
private | same class only |
this referenceRefers to the current object. Used to distinguish a field from a same-named parameter, and to call another overloaded constructor.
A Time class with overloaded constructors (no-arg, hour-only, hour+minute+second) shows how flexible object creation can be.
A class can contain references to objects of other classes as members — modeling real-world "has-a" relationships (a Car has-an Engine, a Student has-an Address) instead of inheriting behavior.
A type-safe, fixed set of named constants — the compiler catches invalid values that a plain int or String "state" would allow by mistake.
Belongs to the class, not any one object — shared across all instances. Call via ClassName.member.
A final instance variable must be assigned exactly once (in a constructor or at declaration) and never changed after.
The JVM automatically reclaims memory from objects that no longer have any references pointing to them — no manual free() needed.
x(), y())equals(), hashCode(), toString() auto-generatedfinal — great for simple, immutable data carriers| Method | Purpose |
|---|---|
toString() | text representation of the object — override for meaningful output |
equals(obj) | content comparison — override to compare by field values, not reference |
hashCode() | integer used by hash-based collections; override alongside equals() |
getClass() | returns the object's runtime Class object |
clone() | creates a copy (class must implement Cloneable) |
Every custom class implicitly extends Object, so these methods are always available — commonly overridden in real-world classes like Account or Time.