A class is the definition of a type with state and behavior, while an object is a concrete instance of that class.
Up to this point in the course, we have been programming in a structured way. We used data types that Java provided (int, double, arrays) and wrote instructions one after another.
But the real world is not made up of just numbers and letters. The world is full of complex things: Users, Cars, Invoices, Video game enemies.
An int cannot represent a “User”. A user has a name, email, age, and behaviors (logging in, buying). We need to create our own data types.
Welcome to Object-Oriented Programming (OOP).
What is a class and what is an object?
This is the fundamental distinction. If you don’t understand this, you don’t understand Java.
A Class is a template, a mold, or a blueprint. It does not exist in the physical world. It is just a theoretical definition of what something should be like.
- Example: The architectural blueprint of a house. The blueprint says where the windows go, but you cannot live in the blueprint.
An Object, or instance, is the materialization of that class. It is something concrete that occupies space in memory (on the Heap).
- Example: The house built at 5 Main Street. You can enter and live in it. With a single blueprint (Class), you can build 100 different houses (Objects).
Defining a class in Java
Let’s create our first class. By convention, classes in Java always start with an Uppercase letter, and each class usually goes in its own .java file.
A class consists of two things:
- Attributes (State): Variables that store data (nouns).
- Methods (Behavior): Functions that do things (verbs).
// File: Robot.java
public class Robot {
// 1. Attributes (Instance variables)
// Describe "what the object is like"
String name;
int batteryLevel;
// 2. Methods
// Describe "what the object can do"
public void greet() {
System.out.println("Hello, I am " + name + " and I have " + batteryLevel + "% energy.");
}
public void charge() {
batteryLevel = 100;
System.out.println("Charge complete. Beep beep.");
}
}Instantiation: the new keyword
Having the code above does nothing by itself. It’s just a blueprint. To use it, we need to create objects. This process is called Instantiating.
To create an instance, we use the keyword new.
// File: Main.java
public class Main {
public static void main(String[] args) {
// Declaration (Stack) = Instantiation (Heap)
Robot r1 = new Robot();
// Configuring object 1
r1.name = "Wall-E";
r1.batteryLevel = 50;
// Creating a SECOND completely independent object
Robot r2 = new Robot();
r2.name = "R2-D2";
r2.batteryLevel = 85;
// Using their behaviors
r1.greet(); // "Hello, I am Wall-E..."
r2.greet(); // "Hello, I am R2-D2..."
}
}What does new actually do?
When you write new Robot(), three critical steps happen in memory:
- Allocation: Java looks for enough free space in the Heap to store a
Stringand anint. - Initialization: Default values are assigned to the attributes (or the constructor is executed, which we will see in the next article).
- Reference: It returns a reference to the object and assigns it to the variable
r1.
The dot operator (.)
You’ve probably noticed r1.name or r1.greet().
The dot is the access operator.
Read it as: “Go to the object that the variable r1 points to and inside it access the attribute name”.
Thanks to this, r1.name and r2.name are different variables, even though they are named the same, because they belong to different instances.
The lifecycle of an object
In the real world, things are born, live, and die. In Java, so do they.
Birth (creation)
This happens when new is executed. The object comes to life on the Heap.
Life (in use)
The object can continue to be used as long as it is reachable from some active reference. It’s not just about counting variables: it can also be connected to other reachable objects.
Robot r3 = new Robot(); // Born
Robot r4 = r3; // Now r4 also points to the same robotDeath and the Garbage Collector
Here, Java shines on its own.
In older languages (C++), if you created an object, you had to destroy it manually (delete). If you forgot, memory would fill up (Memory Leak) and the program would crash.
In Java, there is an automatic process called the Garbage Collector.
When an object is no longer reachable, it becomes available for the garbage collector to reclaim its memory. There is no guarantee it will do so immediately.
r3 = null; // We break the remote control r3
r4 = null; // We break the remote control r4
// At this moment, the Robot object we created is "lost" in the Heap.
// No one can access it.
// The Garbage Collector will pass by, see it, and erase its memory. RIP.