← Back to Week 2 Hub

Intro to Classes & Objects

Why classes exist, what an object is, and how the new keyword turns a blueprint into real data. Walk through it one step at a time.

1. The Problem — Loose Variables Don't Scale
Imagine you're writing a program that tracks people. You already know how to make variables. Let's see what happens as you add more people using only what you've learned so far.
Your code so far
// Tracking one person: String danaName = "Dana"; int danaAge = 63;
2. The Idea — What If We Could Group Them?
The real issue isn't how many variables there are. It's that nothing in the code says these variables belong together. What if we could bundle Dana's name and age into one labeled box called "Person"?
The same data — now grouped
Person
name: "Dana"
age: 63
Person
name: "Natalie"
age: 37
Person
name: "Zachary"
age: 31
Notice: Each box holds one person's data. You can't accidentally mix Dana's name with Natalie's age. Each person's data stays together as a single unit.
This is the core idea of a class. A class is Java's way of saying "data that belongs together." The box itself is a class. Each filled-in box is an object.
3. Blueprint → Objects
The workbook's analogy: model homes built from one blueprint. The blueprint is the class. Each actual house is an object. Click the button to build Person objects from the Person class.
The Class (blueprint)
Person.java
public class Person {
  private String name;
  private int age;

  public Person(String name, int age) {
    this.name = name;
    this.age = age;
  }

  public String getName() {
    return this.name;
  }

  public int getAge() {
    return this.age;
  }
}
Just a description. A class doesn't hold any data by itself. No one is named "Dana" yet — the class just says every Person will have a name and an age.
The Objects (real things)
No objects yet — click the button below to build one.
4. Anatomy Tour — What Every Part Does
Click any highlighted part of the class on the left (or pick from the chips below) to see what it's for, in plain English.
public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } }
Click a highlighted part of the code ← or a chip below to see its explanation.
class declaration field (private) constructor this getter setter
You now know: a class groups related data + actions; an object is one real instance built from that class using new; getters/setters are how outside code reads or changes an object's data.

Next up: why the fields are private in the first place — that's encapsulation, covered in the next visual.