Java OOP · Module 1
Class vs Object
The blueprint and the thing built from it
What you'll be able to do
- Define class and object in your own words, without hand-waving
- Explain why one class can produce thousands of independent objects
- Read `new Phone()` and say exactly what happens in memory
- Predict the default value of a field nobody assigned
Classes
A class is a blueprint
A class is not a thing. It is a description of a kind of thing.
Blueprint → houses
An architect draws one blueprint for a house: three rooms, one door, a colour to be chosen later. Nobody lives in the blueprint. From it, builders create many houses — each with its own colour, its own address, its own furniture. The blueprint is the class; each house is an object.
A class describes attributes and behaviour
Phone. From now on Phone can be used like int or String — as the type of a variable.static here — it needs a specific phone to act on.brand. Two different phones printing here would show two different brands.How to spot a class in the real world
Ask: 'is this a *kind* of thing, or a *specific* thing?' Car is a kind → class. My red 2019 Civic is specific → object. Student is a class; Sara is an object.
Objects
An object is a real thing in memory
What the two lines above look like in memory
Variables live on the stack and hold references (arrows). The objects themselves live on the heap.
Fields you never assign are not empty — they are default
When an object is created, every field gets the default value of its type: 0 for int, 0.0 for double, false for boolean, and null for object types such as String. That is why an unassigned brand prints as null rather than crashing or printing nothing.
Class vs Object
One class, many objects
Class vs Object — the interview answer
| Class | Object | |
|---|---|---|
| What is it? | a blueprint / type | a concrete instance |
| When does it exist? | at compile time, written once | at runtime, created by new |
| Memory | no per-instance data | its own copy of every field |
| How many? | one definition | as many as you create |
| Example | class Student | Sara, Omar, 5,000 more |
Run it, then call `p2.powerOn()` as well and watch `p2.isOn` change while `p1` is untouched.
Common mistakes
Phone p1; p1.brand = "Nokia";Saying 'the class holds the values'.Expecting an unassigned String field to print as empty text.Lesson playground
Create a third phone with your own brand, power it on, and print all three states.
Check understanding
Quiz
Question 1 of 4
Two Car objects are created but only one is filled in. What is printed?
Summary
- A class is a blueprint: it declares fields (state) and methods (behaviour) but stores no values.
- `new` builds one object on the heap and returns a reference to it.
- Every object has its own copy of every field; unassigned fields hold type defaults (0, false, null).
- Declaring a variable is not creating an object — forget `new` and you get NullPointerException.