Classes as blueprints
On This Lesson 11 topics
Lesson Overview
You now know that an object has state and behavior. This lesson answers the next question: where do objects come from? The answer is the class. A class is the blueprint that says which fields and methods exist; an object (also called an instance) is one real thing built from that blueprint.
The difference between class and object is the most important idea of this module, and it trips up many beginners. We will use analogies first, then a short Java preview, then practice telling the two apart.
Why this matters: every backend you read is full of classes (Order, Customer) and the objects created from them while the program runs. Knowing which is which tells you what is written once and what exists many times.
This lesson builds on state, behavior, fields, and methods from the previous lesson, and on arrays and loops from earlier modules. The Java syntax is a preview; the next module teaches it fully.
Learning Objectives
By the end of this lesson you will be able to:
- Define a class as a blueprint and an object as one instance of it.
- Explain that one class can create as many objects as needed, each with its own state.
- Classify a given thing as a class or an object.
- Read a short Java class and a
mainthat creates objects from it. - Explain why a class by itself holds no balances.
- Describe the difference between when a class is written and when objects are created.
Written Explanation
The blueprint and the things built from it
Think of a form template. The template says: this form has a name field, a date field, and a signature box. It is written once. When 200 people fill it in, you get 200 filled-in forms. Each has the same shape, but different values.
The Java tutorial uses a bicycle: many bicycles of the same model are built from the same set of blueprints, and each real bicycle is an instance of the class of bicycles. The same applies in software:
| Class | Object (instance) | |
|---|---|---|
| What it is | The blueprint: it defines which fields and methods exist | One real thing built from the blueprint |
| Written | Once, by the programmer, in the source code | Created while the program runs |
| Values | None of its own (it only declares the fields) | Has its own values in each field |
| How many | One definition | As many as needed |
| Example | Account | Ana's account, Omar's account |
Instance and object mean the same thing in this course. "Creating an object" is also called "instantiating the class".
A class in Java (preview)
class Account {
String owner;
int balance;
void deposit(int amount) { balance += amount; }
void describe() { System.out.println(owner + ": " + balance); }
}This declares what an account is: it has an owner, a balance, and two behaviors. It does not create any account. No balance exists yet. It is only the blueprint.
To get actual accounts, use the keyword new:
Account ana = new Account();
Account omar = new Account();Each new Account() builds a separate object. The Java tutorial describes new as allocating memory for the new object and returning a reference to it. The variable ana holds a reference to one object; omar holds a reference to another. Declaring a variable like Account x; alone does not create an object.
The dot connects a reference to the object's fields and methods: ana.balance, ana.deposit(2500).
One class, many objects, separate state
public class Two {
public static void main(String[] args) {
Account ana = new Account();
ana.owner = "Ana";
ana.balance = 10000;
Account omar = new Account();
omar.owner = "Omar";
omar.balance = 5000;
ana.deposit(2500);
ana.describe();
omar.describe();
}
}Output:
Ana: 12500
Omar: 5000The same deposit code ran, but only on ana. Omar's balance stayed at 5000. The class is shared; the state is not.
You can also create many objects in a loop and keep them in an array, using arrays from BEG-015:
Account[] all = new Account[3];
for (int i = 0; i < all.length; i++) {
all[i] = new Account();
all[i].owner = "Client" + i;
}
System.out.println(all[2].owner + " " + all[2].balance);Output: Client2 0. The balance is 0 because fields of a new object get a default value: a run on JDK 21 shows 0 for int, null for a String, and false for a boolean. (Note that new Account[3] creates an array of three empty slots; each new Account() fills one.)
This is the payoff compared to parallel arrays: 3 or 3,000 accounts, one definition.
Analogies, and where they break
- Cookie cutter and cookies: the cutter is the class; each cookie is an object. Each cookie can get its own icing (state).
- Architect's plan and houses: one plan, many houses, each with its own occupants.
- Recipe and cakes: the recipe is not edible; only the cakes are.
No analogy is perfect. A cookie cutter is a physical thing, while a class is only a description in your code; and in Java the class is also loaded and used by the running program. For this lesson the rule is enough: the class describes, the object holds values.
Quick classification rule
If you can say "one of many", it is probably an object. If you can say "the kind of thing", it is a class.
Accountis a kind of thing: class.- "Ana's account" is one particular account: object.
- "Customer" is a kind: class. "The customer named Omar": object.
Examples
Example 1: forms and cookies (simple)
Fill in the table by deciding class or object:
| Phrase | Class or object? |
|---|---|
| "a passport application form" (the design) | class |
| "the form Sara filled in on Monday" | object |
| "Book" | class |
| "the copy of Dune on shelf 3" | object |
The same property list (title, author) describes every book; only an actual copy has actual values. Note that "object" here means one specific thing, not necessarily a physical one: a booking or a login session is an object too.
Example 2: two accounts from one class (practical)
The Two program above is the example. Study three points:
- The class
Accountappears once in the file. new Account()appears twice, so there are two objects.- A change to
anadoes not affectomar.
Now a trap that shows the difference between class and object. Suppose you try to set the balance on the class name instead of on an object:
Account.balance = 5;The compiler answers:
Mistake.java:5: error: non-static variable balance cannot be referenced from a static context
Account.balance = 5;
^The message uses the word static because Java also lets you attach things to the class itself (the next module covers static). For now, read it as "balance belongs to individual objects; the class alone has no balance". You must name an object, as in ana.balance = 5;.
Example 3: a hundred orders from one blueprint (realistic)
A shop's backend might hold this idea:
class Order {
int number;
int totalCents;
boolean paid;
}At 9:00 there are no Order objects. At 9:05 a customer places an order and the program runs new Order(). By evening there are hundreds, each with its own number, total, and paid flag. The class Order was written once, weeks earlier. At night the program stops, and the objects in memory are gone (their data is usually saved to a database first; that is a later topic). The blueprint remains in the source code for tomorrow.
This shows the timeline from the table: classes are written at development time, objects are created at run time.
Backend Insight
In a Java backend, the classes in your project are the vocabulary of the business: Customer, Order, Payment. What the server does all day is create, use, and discard objects of those classes, one per customer, request, or order. Two things follow.
First, a bug can live in the blueprint (every object is wrong) or in one object's state (only Ana's balance is wrong). Telling the two apart is the first step of debugging: ask whether all objects misbehave or just one.
Second, performance and memory thinking depends on this distinction. A class is defined once; ten thousand objects occupy ten thousand times the space for their fields. You do not need numbers now; what you need is the habit of asking "how many objects will exist at once?"
Common Mistakes
Mistake 1: Using "class" and "object" as synonyms
- Incorrect: "My account class has a balance of 5000."
- Correct: "My
Accountclass declares abalancefield. Ana's account object has a balance of 5000."
Mistake 2: Expecting a class to hold data
A class declares fields; it does not hold values for them. The compiler error in Example 2 is exactly this mistake.
Mistake 3: Thinking Account x; creates an account
Declaring a variable only creates a place for a reference. The object appears when new Account() runs. (A default null reference points to no object at all; using it to call a method crashes at run time, a topic from the debugging module.)
Mistake 4: Thinking assignment copies an object
Account b = a; // b and a now refer to the SAME object
b.balance = 7;
System.out.println(a.balance);A run printed 7. Assigning a reference copies the reference, not the object. Only new builds a new object. This is the same reference idea you met with arrays in BEG-014 and BEG-015.
Exercises
Try each exercise yourself before opening the solutions.
Level A: Foundation
A1. Complete the sentence: a class is a ____, and an object is ____.
Show solution A1
A1. A blueprint (a definition of fields and methods); one real thing built from that blueprint.
A2. Class or object? Student, the student with ID 4021, Playlist, my "Gym Mix" playlist, Ticket, the ticket numbered 88.
Show solution A2
A2. Class, object, class, object, class, object.
A3. How many objects can one class create?
Show solution A3
A3. As many as the program needs (limited only by available memory).
Level B: Application
B1. Explain class versus object using an analogy that is not a cookie cutter, a recipe, or an architect's plan.
Show solution B1
B1. Example: a music CD design versus a pressed CD. The design (track list) is the class; each disc in a shop is an object. Another valid analogy is a form template and filled-in forms.
B2. For a class Book with title and isBorrowed, write (as code or pseudocode) two objects with different titles. Which part is the class and which part is the objects?
Show solution B2
B2. Example:
class Book { String title; boolean isBorrowed; }
// objects:
Book b1 = new Book(); b1.title = "Dune";
Book b2 = new Book(); b2.title = "Emma";The class Book block is the class; b1 and b2 refer to the two objects.
B3. A program has class Account, then runs new Account() four times in a loop and saves them in an array. How many class definitions and how many objects exist?
Show solution B3
B3. One class definition and four objects.
Level C: Challenge
C1. Write and run a program with a class Lamp (field isOn) and three lamp objects in an array. Switch on only the second one and print all three states. Record the real output.
Show solution C1
C1. Example:
public class Lamps {
public static void main(String[] args) {
Lamp[] lamps = new Lamp[3];
for (int i = 0; i < lamps.length; i++) lamps[i] = new Lamp();
lamps[1].isOn = true;
for (Lamp l : lamps) System.out.println(l.isOn);
}
}
class Lamp { boolean isOn; }Output: false, true, false, one per line.
C2. Explain what the following prints and why: Lamp x = new Lamp(); Lamp y = x; y.isOn = true; System.out.println(x.isOn);
Show solution C2
C2. It prints true. y = x copies the reference, so both variables point at one object, and changing it through y is visible through x.
Mini Challenge
Choose a domain you know (a gym, a bus company, a school). Name three classes. For each class:
- Write two or three fields and one behavior.
- Give two example objects with different values.
- Say how many objects of this class you expect to exist at the same time, roughly.
Then write one sentence explaining, for one of your classes, which of your statements describes the class and which describes objects.
Check your work: no value (like "Ana") appears in a class description, no kind of thing appears as an object, and your sentence uses the words blueprint and instance.
Summary & Next Lesson
Key takeaways
- A class is the blueprint; an object (instance) is one real thing built from it.
- One class can create as many objects as needed, each with its own state.
- A class is written once, at development time; objects are created at run time with
new. - A class alone holds no values; compile errors and bugs often come from forgetting that.
- Assigning an object variable copies the reference, not the object.
Next lesson
Next is "Finding objects in a problem description". You will learn a first-guess technique for deciding which classes a problem needs, by reading the text and underlining nouns and verbs.
Self-check
- What is the difference between a class and an object?
- How many objects can one class create?
- Is
Customera class or an object? What about "the customer named Omar"? - Does
Account x;create an account? - Why does a change to Ana's account not change Omar's?
Show answers
- A class is the blueprint that declares fields and methods; an object is one instance built from it, with its own values.
- As many as needed.
Customeris a class; "the customer named Omar" is an object.- No. It only declares a variable that can refer to an account;
new Account()creates one. - They are two separate objects, each with its own state.
References
- Oracle, The Java Tutorials: What Is a Class?. Supports the blueprint idea: a class is a blueprint, each object is an instance of it, and each instance keeps its own state.
- Oracle, The Java Tutorials: Creating Objects. Supports that
newinstantiates a class, allocates memory, returns a reference, and that declaring a variable alone does not create an object. - All code outputs and the compiler message were produced with JDK 21 (javac 21.0.10). The
Lampssolution and the C2 snippet were compiled and run too.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.