Class vs object in Java
A class is a blueprint; objects are instances created with new.
Blueprint vs house
A class is a blueprint: it describes what data (*fields*) and behavior (*methods*) its objects will have. An object is one concrete *instance* built from that blueprint at runtime. Declaring a class creates zero objects — just like a drawing isn't a house.
class Dog {
String name;
void bark() {
System.out.println("Woof");
}
}`new` builds the house
new Dog() allocates an object and runs a constructor. The variable doesn't hold the house itself — it holds a reference, like the house's address. One class can produce any number of independent objects.
Dog rex = new Dog(); // object #1
Dog fido = new Dog(); // object #2
rex.name = "Rex";Two houses, one blueprint
What does this print?
class Cup { String drink; }
void main() {
Cup a = new Cup();
Cup b = new Cup();
a.drink = "tea";
b.drink = "coffee";
System.out.print(a.drink + " ");
System.out.println(b.drink);
}coffee coffeetea coffeetea tea
Show the answer
tea coffee. a and b are separate objects built from the same class, and each keeps its own copy of the drink field.
Object or not?
Which of these are objects: "hello", new int[3], new StringBuilder(), 42?
Think about it, then reveal the answer
The first three are objects — Strings and arrays are objects even though they have special syntax. **42 is not**: it's a primitive int value, stored directly with no object around it.
An array of boxes holds no boxes
new Box[3] creates one array with three empty slots — not three Box objects. Every slot holds the default reference value null until you put a new Box() in it.
Box[] boxes = new Box[3];
System.out.println(boxes[0]); // null
boxes[0].v = 1; // NPE!
boxes[0] = new Box(); // now a BoxIn real projects
Every app is built from classes like User, Order and Invoice, and every request creates fresh objects from them. Frameworks such as Hibernate turn each database row into an object of your class — one blueprint, millions of instances.
Key takeaways
- A class is a type; an object is an instance of it
- new allocates an object and runs a constructor
- Each object has its own copy of the instance fields
- Declaring a class creates no objects by itself
💡 A class is a cookie cutter; objects are the cookies. One cutter, many cookies.
Classes are objects too, at runtime: Dog.class is an instance of java.lang.Class that describes the Dog blueprint.
Practice questions
What does this print?
class Dog { String name; }
void main() {
Dog a = new Dog();
Dog b = new Dog();
a.name = "Rex";
b.name = "Fido";
System.out.println(a.name + " " + b.name);
}- Fido Fido
- Rex Fido
- Rex Rex
- null Fido
Check your answer
Rex Fido. a and b are two separate objects built from the same class, so each keeps its own name.
Which of these is NOT an object?
- new int[3]
- "hello"
- new StringBuilder()
- 42
Check your answer
42. Arrays and Strings are objects even though they have special syntax. 42 is a primitive int value, not an object.