JAVA — CHAPTER 8

Real-World Modeling with Custom Classes · Concept Cheat Sheet
Encapsulation Constructors this Composition enum static record
1 INSTANCE VARIABLES, set & get METHODS
public class Account { private String name; // instance variable public void setName(String name) { this.name = name; } public String getName() { return name; } }

Encapsulation

  • Make fields private — hide internal data
  • Expose controlled access through public set/get methods
  • set methods can validate input before storing it
2 CONSTRUCTORS & INITIALIZATION
public class Account { private String name; private double balance; public Account(String name) { this(name, 0.0); // calls the other constructor } public Account(String name, double balance) { this.name = name; this.balance = balance; } }

Key ideas

  • A constructor runs once, when an object is created with new
  • Same name as the class, no return type
  • Overloaded constructors give multiple ways to build an object
  • this(...) calls another constructor in the same class
  • No-argument default constructor is provided automatically only if you declare none yourself
3 ACCESS CONTROL & THE this REFERENCE

Access modifiers

ModifierVisible to
publiceveryone
protectedpackage + subclasses
(none) packagesame package only
privatesame class only

this reference

Refers to the current object. Used to distinguish a field from a same-named parameter, and to call another overloaded constructor.

Case study idea

A Time class with overloaded constructors (no-arg, hour-only, hour+minute+second) shows how flexible object creation can be.

4 COMPOSITION — "HAS-A" RELATIONSHIPS
public class Student { private Address address; // Student HAS-A Address }

What it means

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.

5 enum TYPES
public enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY } Day today = Day.MONDAY; switch(today) { case MONDAY -> System.out.println("Start of week"); }

Why enums?

A type-safe, fixed set of named constants — the compiler catches invalid values that a plain int or String "state" would allow by mistake.

6 static MEMBERS, final & GARBAGE COLLECTION

static

Belongs to the class, not any one object — shared across all instances. Call via ClassName.member.

final

A final instance variable must be assigned exactly once (in a constructor or at declaration) and never changed after.

Garbage collection

The JVM automatically reclaims memory from objects that no longer have any references pointing to them — no manual free() needed.

7 record CLASSES — MODERN, CONCISE DATA CLASSES
public record Point(int x, int y) { } Point p = new Point(3, 4); System.out.println(p.x() + ", " + p.y());

What you get for free

  • Constructor, accessor methods (x(), y())
  • equals(), hashCode(), toString() auto-generated
  • Fields are implicitly final — great for simple, immutable data carriers
8 METHOD REFERENCE TABLE — class Object (inherited by every class)
MethodPurpose
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.