Classes And Objects
Classes & Objects: Overview
Chapter 3 of the Java book, the leap to objects. Four lessons take you from procedural code to classes: defining them, hiding state behind a controlled interface, the class-vs-instance split, and the reference model (stack and heap, == vs .equals, null) behind every Java surprise. Every example is compiled and run.
Suggest an editClasses & Objects: Overview
Welcome to Chapter 3, where Java becomes object-oriented. Control Flow gave you decisions, loops, arrays, and methods — the procedural building blocks. Now the unit of design changes: instead of methods acting on loose data, you bundle data and behavior together into classes, and build objects from them. The chapter ends with the lesson everything else rests on — the reference model — which turns the "surprises" of earlier chapters into predictable consequences.
There are four lessons, in this order:
- Defining Classes & Creating Objects — fields and methods bundled into a blueprint,
new, constructors,this, and independent instances. - Encapsulation & Access Modifiers —
private, getters/setters that enforce invariants, the four access levels, and immutability by design. staticvs Instance — class-level shared state vs per-object state, why astaticmethod has nothis,static finalconstants, andstaticblocks.- References, Equality & the Object Model — the flagship: stack vs heap, values vs references,
==vs.equals(with the Integer cache and String pool), andnull.
Every code block with a ▶ Run button is live. In this chapter, the most valuable habit you can build is to draw the references: whenever an example aliases two variables or compares two objects, sketch which variables point at which heap objects before you predict the output. The reference-model lesson gives you that picture explicitly.
📘 How to read the Intuition boxes. Each one is built in three moves:
- The mechanism — what the compiler and the JVM are actually doing.
- A concrete bite — a specific, runnable failure (often a real compiler error), shown so the trap is visible.
- The earned rule — the decision heuristic, now justified rather than asserted, plus its cost.