Skip to content

Java OOP

0/36 lessons · 0% complete

— XP

Java OOP · Module 1

Class vs Object

The blueprint and the thing built from it

14m35 XP
→→→

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
1

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

Main.java
1class Phone {
2 String brand;
3 boolean isOn;
4
5 void powerOn() {
6 isOn = true;
7 System.out.println(brand + " is on");
8 }
9}
class Phone {
Declares a new type called Phone. From now on Phone can be used like int or String — as the type of a variable.
String brand;
A field (also called attribute): a piece of data that every Phone object will have its own copy of. Notice there is no value here — the blueprint only says *what* exists.
boolean isOn;
A second field. Fields together form the object's state.
void powerOn() {
A method: behaviour that belongs to a Phone. No static here — it needs a specific phone to act on.
isOn = true;
Inside a method, a bare field name means 'my own field' — the field of the object the method was called on.
System.out.println(brand + " is on");
Reads that same object's 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.

2

Objects

An object is a real thing in memory

Creating two independent phones
Main.java
1Phone p1 = new Phone();
2p1.brand = "Nokia";
3
4Phone p2 = new Phone();
5p2.brand = "Pixel";
`new Phone()` builds one object and hands back its address. `p1` and `p2` refer to two separate objects, each with its own brand and isOn.

What the two lines above look like in memory

Stack
p1→ref → #1
p2→ref → #2
Heap
#1: Phone
brand"Nokia"
isOnfalse
#2: Phone
brand"Pixel"
isOnfalse

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.

3

Class vs Object

One class, many objects

Class vs Object — the interview answer

ClassObject
What is it?a blueprint / typea concrete instance
When does it exist?at compile time, written onceat runtime, created by new
Memoryno per-instance dataits own copy of every field
How many?one definitionas many as you create
Exampleclass StudentSara, Omar, 5,000 more
Two objects, two states

Run it, then call `p2.powerOn()` as well and watch `p2.isOn` change while `p1` is untouched.

Output
Press Run to execute this example.

Common mistakes

WrongPhone p1; p1.brand = "Nokia";
WhyDeclaring a variable does not create an object. `p1` holds nothing yet, so touching `.brand` throws NullPointerException.
FixPhone p1 = new Phone(); — `new` is what actually builds the object.
WrongSaying 'the class holds the values'.
WhyThe class only describes which fields exist; the values live in each object.
FixBlueprint describes, object stores.
WrongExpecting an unassigned String field to print as empty text.
WhyIts default value is `null`, and `null + " is on"` prints `null is on`.
FixAssign fields when you create the object — Module 3 shows the proper way: a constructor.

Lesson playground

Free practice

Create a third phone with your own brand, power it on, and print all three states.

Output
Press Run to execute this example.

Check understanding

Quiz

Question 1 of 4

Two Car objects are created but only one is filled in. What is printed?

Main.java
1class Car {
2 String model;
3 int speed;
4}
5
6public class Main {
7 public static void main(String[] args) {
8 Car a = new Car();
9 a.model = "Civic";
10
11 Car b = new Car();
12
13 System.out.println(a.model);
14 System.out.println(b.model);
15 System.out.println(b.speed);
16 }
17}

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.