Objects and Classes
Your first Java class, instances, fields, and methods — and the difference between a blueprint and the thing built from it
A class is a blueprint. An object is a thing built from that blueprint. This page is a slow, careful look at the difference, because almost every confusion in OOP starts with mixing those two words up.
The class is the description, the object is the thing
Imagine an architect's blueprint for a house. The blueprint says "three bedrooms, two bathrooms, a kitchen." You cannot live in the blueprint. You can only live in a house built from the blueprint. And from the same blueprint you can build many houses — same shape, different addresses, different colors, different families inside.
In Java, the keyword class declares a blueprint. The keyword new
builds an object from it. Each object has its own copy of the class's
fields (state), and the class defines what methods every object
of that type can respond to.
Notice:
class Dog { ... }is the blueprint.new Dog("Rex", 4)produces an instance (an object).rexandlunaare two different objects. They share the blueprint but each has its ownnameandage.rex.haveBirthday()changesrex.age. It does not touchluna.age.
That last bullet point is the entire reason classes are useful. Each object has its own state.
The anatomy of a class
A class declaration typically has four sections, in this order by convention:
| Part | Purpose |
|---|---|
| Fields | The state every instance carries |
| Constructor(s) | How to bring a valid new instance into existence |
| Methods | The behavior — the messages instances respond to |
| (sometimes) Static members | Things that belong to the class as a whole, not to instances |
We will meet static members in a later page. For now, almost everything you write will be a field, a constructor, or a method.
Multi-file classes: one class per file
Real Java programs put each top-level public class in its own file, with the filename equal to the class name. We will follow that convention from now on. CheerpJ in your browser does the same.
A few things are worth pointing out in Book:
- The fields are
private final. Private means no code outsideBookcan read or write them directly. Final means once a field is assigned in the constructor, it cannot be reassigned. We will study both ideas in detail in Encapsulation. - The methods
title(),author(),year()are sometimes called accessors or getters — they answer questions about the object's state. olderThan(Book other)takes anotherBookas a parameter and compares the two. Inside that method,this.yearrefers to this particular book's year, whileother.yearrefers to the argument's. (other.yearis allowed because the comparison is happening inside theBookclass itself.)
this: the object the method is running on
Every non-static method has an implicit parameter called this. It is
the object the method was called on. When you write
rex.describe(), inside describe, this is rex. When you write
luna.describe(), inside the same method body, this is luna.
You usually don't have to type this — Java assumes it — but you
must type it when you have a parameter or local variable with the
same name as a field, which is why constructors often look like
this.name = name;.
Beginner-friendly objects exercise
Create a Rectangle with a width and height (both double). It must support area() and perimeter() methods.
Main.java is already wired up — it constructs a Rectangle with width 4 and height 3 and prints two lines:
area: 12.0
perimeter: 14.0
Two objects, one class
This challenge introduces a second object to drive home that each instance has its own state.
Implement the Counter class with a private int count initialized to 0, a bump() method that increases the count by 1, and a value() method that returns the current count. The provided Main creates two counters and prints their values; they must be independent.
Expected output:
a=3, b=1
Test your understanding
What is the difference between a class and an object in Java?
They are exactly the same thing
An object is a description; a class is built from it
A class is a blueprint that describes structure and behavior; an object (instance) is an individual thing created from that blueprint
A class can have state but an object cannot
If you write Dog a = new Dog("Rex", 4); and Dog b = new Dog("Luna", 2);, and then call a.haveBirthday();, what happens to b.age?
It is also incremented because all dogs share the field
It is set to 0
It stays the same — each object has its own copy of the fields
The program raises an error
Inside an instance method, what does the keyword this refer to?
The class itself (the blueprint)
A copy of every other object of the same class
The specific object the method was called on
A field automatically named this