What Is a Class? Blueprint for Objects
You'll learn: How classes serve as templates that define what objects will look like and how they'll behave.
The Core Idea
A class is like a blueprint or template in Java. Just as an architect creates a blueprint for a house before any houses are built, you write a class definition before creating any objects. The class specifies:
- What data each object will hold (attributes/fields)
- What actions each object can perform (methods)
Think of a class as a cookie cutter and objects as the actual cookies. The cookie cutter defines the shape, but you can make many individual cookies from that one cutter.
Classes vs Objects
When you write a class, you're not creating an object—you're describing what objects of that type will be like. You've already been using objects created from classes: String is a class, and every text value you create (like "Hello") is an object of that class. Arrays are objects too, created from special array classes.
The class answers: "What should every object of this type have?" Each object created from the class then gets its own copy of the data, stored on the heap (as you learned in lesson 294).
Why Classes Matter
Classes let you define your own custom types beyond Java's built-in primitives and standard library classes. You can model real-world concepts—like a BankAccount, Student, or Product—by creating a class that bundles related data and behavior together.
Key Takeaway: A class is a blueprint that defines the structure (data) and capabilities (methods) for objects; you use new to create actual object instances from that blueprint.