Understanding Classes and Objects in Java
Classes and objects are the building blocks of every Java program. A class is a blueprint; an object is a concrete instance of that blueprint.
Class = Blueprint
A class defines:
- What data an object will hold (fields)
- What behavior an object will have (methods)
class Dog {
// Fields (data)
String name;
String breed;
int age;
// Methods (behavior)
void bark() {
System.out.println(name + " says Woof!");
}
void describe() {
System.out.printf("%s is a %d-year-old %s%n", name, age, breed);
}
}
A class doesn’t occupy object memory by itself — it’s just the definition.
Object = Instance
An object is a specific instance created from a class:
Dog dog1 = new Dog();
dog1.name = "Buddy";
dog1.breed = "Golden Retriever";
dog1.age = 3;
Dog dog2 = new Dog();
dog2.name = "Max";
dog2.breed = "German Shepherd";
dog2.age = 5;
dog1.bark(); // Buddy says Woof!
dog2.describe(); // Max is a 5-year-old German Shepherd
Each object has its own copy of the fields. Changing dog1.name doesn’t affect dog2.name.
Memory Model
Stack Heap
┌──────────┐ ┌──────────────────┐
│ dog1 ────────→ │ name: "Buddy" │
│ │ │ breed: "Golden.."│
│ │ │ age: 3 │
│ │ └──────────────────┘
│ dog2 ────────→ ┌──────────────────┐
│ │ │ name: "Max" │
│ │ │ breed: "German.."│
│ │ │ age: 5 │
└──────────┘ └──────────────────┘
- Stack: holds the reference variable (
dog1,dog2) - Heap: holds the actual object data
Multiple Objects, One Class
You can create unlimited objects from one class:
Dog[] kennel = new Dog[100];
for (int i = 0; i < 100; i++) {
kennel[i] = new Dog();
kennel[i].name = "Dog #" + i;
}
Null References
A reference variable can point to nothing:
Dog dog = null; // no object yet
dog.bark(); // NullPointerException!
Always check for null before using an object:
if (dog != null) {
dog.bark();
}
Employee Management Example
Our running example throughout this tutorial:
class Employee {
String name;
String department;
double salary;
void displayInfo() {
System.out.printf("%-15s %-12s $%,.2f%n", name, department, salary);
}
}
// Usage
Employee emp1 = new Employee();
emp1.name = "Alice Johnson";
emp1.department = "Engineering";
emp1.salary = 95000;
Employee emp2 = new Employee();
emp2.name = "Bob Smith";
emp2.department = "Marketing";
emp2.salary = 78000;
System.out.printf("%-15s %-12s %s%n", "Name", "Department", "Salary");
emp1.displayInfo();
emp2.displayInfo();
Quick Exercises
- Create a
Bookclass withtitle,author,pages. Add a methodsummary()that prints all fields. - Create 3
Bookobjects and store them in an array. Print all summaries in a loop. - What happens when you access a field of a
nullreference?
Summary
- Class = blueprint (defines fields + methods)
- Object = instance (created with
new) - Each object has its own copy of fields
- References live on the stack; objects live on the heap
nullmeans “no object” — accessing it throwsNullPointerException
What’s Next?
Let’s learn the syntax for Defining Classes in Java properly — access modifiers, naming, and structure.