Understanding Classes and Objects in Java
Understanding Classes and Objects in Java
Introduction#
In the previous article, we explored Java fundamentals such as comments, identifiers, keywords, and data types.
Now it's time to learn one of the most important concepts in Java: Classes and Objects.
Java is an Object-Oriented Programming (OOP) language, and classes and objects form the foundation of OOP. Almost every Java application is built using these concepts.
In this article, we will understand what classes and objects are, why they are needed, and how to create them using simple examples.
What is Object-Oriented Programming (OOP)?#
Object-Oriented Programming is a programming approach where we model real-world entities as objects.
Instead of writing everything as a sequence of instructions, we organize data and behavior together into objects.
Some real-world objects are:
- Student
- Car
- Mobile Phone
- Bank Account
- Employee
Each object has:
- Properties (data)
- Behaviors (actions)
For example, a Student has:
Properties
- Name
- Age
- Roll Number
Behaviors
- Study
- Attend Class
- Write Exam
This idea forms the basis of OOP.
What is a Class?#
A class is a blueprint or template used to create objects.
It defines what data an object will have and what actions it can perform.
Real-Life Analogy#
Imagine a car manufacturing company.
Before making a car, engineers create a design or blueprint.
That blueprint contains details such as:
- Engine type
- Number of seats
- Fuel capacity
The blueprint itself is not a car.
It is simply a design used to create cars.
Similarly:
A Class is a blueprint, not a real object.
Creating a Class in Java#
Let's create a simple Student class.
class Student {
String name;
int age;
}
Here:
Studentis the class name.nameandageare data members (attributes).
The class only defines the structure of a student.
No actual student exists yet.
What is an Object?#
An object is a real instance of a class.
If a class is a blueprint, then an object is the actual thing built using that blueprint.
Real-Life Analogy#
Consider a blueprint for a house.
Using the same blueprint, multiple houses can be built.
- Blueprint → Class
- House → Object
Similarly:
- Student → Class
- Ravi → Object
- Priya → Object
Both Ravi and Priya are created from the same Student class but contain different data.
Creating Objects in Java#
Objects are created using the new keyword.
class Student {
String name;
int age;
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Ravi";
s1.age = 20;
System.out.println(s1.name);
System.out.println(s1.age);
}
}
Output#
Ravi
20
Here:
Studentis the class.s1is the object.new Student()creates memory for the object.
Understanding the Syntax#
Student s1 = new Student();
Let's break this statement down.
Student#
Represents the class.
Student
s1#
Represents the object reference.
s1
new Student()#
Creates a new object in memory.
new Student()
Together:
Student s1 = new Student();
creates a Student object and stores its reference in s1.
Creating Multiple Objects#
One class can create multiple objects.
class Student {
String name;
int age;
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Ravi";
s1.age = 20;
Student s2 = new Student();
s2.name = "Priya";
s2.age = 21;
System.out.println(s1.name + " is " + s1.age + " years old");
System.out.println(s2.name + " is " + s2.age + " years old");
}
}
Output#
Ravi is 20 years old
Priya is 21 years old
Both objects are created from the same class but store different values.
Class vs Object#
| Class | Object |
|---|---|
| Blueprint or template | Real instance of a class |
| Defines structure | Holds actual data |
| Created once | Can be created many times |
| Does not occupy memory for individual data | Occupies memory when created |
Example#
| Class | Objects |
|---|---|
| Student | Ravi, Priya, Rahul |
| Car | BMW, Audi, Tesla |
| Mobile | iPhone, Samsung, OnePlus |
Real-World Example: Car#
Let's understand classes and objects using a Car example.
class Car {
String brand;
String color;
}
public class Main {
public static void main(String[] args) {
Car car1 = new Car();
car1.brand = "Toyota";
car1.color = "White";
Car car2 = new Car();
car2.brand = "Honda";
car2.color = "Black";
System.out.println(car1.brand + " - " + car1.color);
System.out.println(car2.brand + " - " + car2.color);
}
}
Output#
Toyota - White
Honda - Black
Notice that both cars belong to the same class but contain different information.
Why Do We Need Classes and Objects?#
Classes and objects help us:
- Organize code better
- Represent real-world entities
- Reuse code efficiently
- Build large applications more easily
- Follow Object-Oriented Programming principles
Without classes and objects, managing large programs becomes difficult.
How Are Classes and Objects Stored in Memory?#
When a Java program runs, memory is allocated for both classes and objects.
Let's look at the following example:
class Student {
String name;
int age;
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Ravi";
s1.age = 20;
Student s2 = new Student();
s2.name = "Priya";
s2.age = 21;
}
}
Where Is the Class Stored?#
When the program starts, the JVM loads the Student class into a special memory area called the Method Area (also known as the Class Area).
The class definition is loaded only once, regardless of how many objects are created.
Method Area
Student
├── name
└── age
Where Are Objects Stored?#
Objects are created inside Heap Memory.
Every time we use the new keyword, a new object is created in the heap.
Student s1 = new Student();
Student s2 = new Student();
Memory representation:
Heap Memory
s1 Object
├── name = Ravi
└── age = 20
s2 Object
├── name = Priya
└── age = 21
Each object gets its own copy of the data.
This means changing one object does not affect another object.
For example:
s1.name = "Rahul";
Only the data inside s1 changes.
The values stored in s2 remain the same.
Quick Summary#
Class → Method Area (loaded once)
Objects → Heap Memory (created using new)
Understanding this memory model is important because in the next article we will learn:
- Instance Variables
- Static Variables
- Local Variables
and see how each of them is stored and managed in memory.
Check Your Understanding#
1. A blueprint for a house can be compared to which Java concept: Class or Object? Explain why.
2. How many objects can be created from a single class? Give a real-world example.
3. Consider the following code:
Student s1 = new Student();
Student s2 = new Student();
Are s1 and s2 the same object or different objects? Explain.