Introduction
Java is a class-based, object-oriented programming language, and everything in Java revolves around classes and objects ☕
To write clean, structured, and maintainable Java programs, it is essential to clearly understand what classes are, what objects are, and how methods work.
These three concepts form the foundation of Object-Oriented Programming (OOPs).
If this foundation is strong, all advanced Java concepts become much easier to understand 💡
This article explains Classes, Objects, and Methods in depth, using complete runnable code, step-by-step explanations, and common mistakes to avoid.
🧠 What Is a Class in Java?
A class is a blueprint or template used to create objects 🏗️
It defines:
- What data an object will store (variables)
- What actions an object can perform (methods)
A class itself:
- ❌ Does NOT execute
- ❌ Does NOT occupy memory
- ✅ Only defines structure
🏗️ Defining a Class
class Student {
int rollNo;
String name;
void display() {
System.out.println("Roll No: " + rollNo);
System.out.println("Name: " + name);
}
}
🧠 Explanation
class Student→ defines a blueprint namedStudentrollNo,name→ instance variables (each object gets its own copy)display()→ instance method- No memory is allocated at this stage ❗
🎯 What Is an Object in Java?
An object is a real instance of a class 🎯
It represents a real-world entity created from the class blueprint.
Objects:
- Occupy memory
- Store actual data
- Call methods
🧠 The new Operator (Very Important)
Objects are created using the new operator.
What happens internally when new is used? 🔍
1️⃣ Memory is allocated in heap memory
2️⃣ Constructor is called
3️⃣ Object reference is returned
▶️ Complete Runnable Program (Class + Object + Method)
class Student {
int rollNo;
String name;
void display() {
System.out.println("Roll No: " + rollNo);
System.out.println("Name: " + name);
}
}
public class StudentDemo {
public static void main(String[] args) {
Student s1 = new Student();
s1.rollNo = 101;
s1.name = "Ajay";
s1.display();
}
}
🧠 Line-by-Line Explanation
🔹 Student s1 = new Student();
Student→ class names1→ reference variable (stored in stack)new Student()→ object created in heap
🔹 s1.rollNo = 101;
- Assigns value to the object’s variable
🔹 s1.display();
- Calls the method using the object reference
🧠 Memory Concept (Important for Understanding)
- Reference variable (
s1) → stored in stack - Object → stored in heap
- Reference holds the address of the object
📌 Multiple references can point to the same object.
🔍 Creating Multiple Objects
public class MultipleObjectsDemo {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
s1.rollNo = 101;
s1.name = "Ajay";
s2.rollNo = 102;
s2.name = "Rahul";
s1.display();
s2.display();
}
}
🧠 Each object has its own copy of instance variables.
⚙️ What Is a Method?
A method is a block of code that:
- Performs a specific task
- Is associated with a class
- Is executed using an object (for non-static methods)
✍️ Method Example
class Calculator {
int add(int a, int b) {
return a + b;
}
}
public class MethodDemo {
public static void main(String[] args) {
Calculator c = new Calculator();
int result = c.add(10, 20);
System.out.println("Result: " + result);
}
}
🧠 How Method Execution Works
- Method call transfers control to method
- Parameters receive values
- Method executes logic
- Return value is sent back
🔁 Calling Methods Multiple Times
c.add(5, 10);
c.add(20, 30);
Each call is independent.
⚠️ Common Confusions (Very Important)
❓ Class vs Object
- Class → blueprint 🏗️
- Object → real entity 🎯
❓ Why object is required to call methods?
Because instance methods belong to objects, not classes.
❓ Why main() is static?
So JVM can call it without creating an object.
❓ Reference variable vs Object
- Reference variable stores address
- Object stores data
❌ Common Mistakes
❌ Forgetting to create an object
display(); // ❌ ERROR
✔️ Correct:
s1.display();
❌ Accessing instance variables without object
rollNo = 10; // ❌ ERROR
✔️ Correct:
s1.rollNo = 10;
❌ Writing everything inside main()
This defeats the purpose of OOPs ❌
🧪 Tricky Code Example (Understanding References)
Student s1 = new Student();
Student s2 = s1;
s1.rollNo = 100;
System.out.println(s2.rollNo);
🖨️ Output:
100
🧠 Explanation:
Both references point to the same object.
📌 Key Takeaways
- Class defines structure
- Object represents reality
- Methods define behavior
newcreates objects in heap- Reference variables point to objects
- Strong understanding here simplifies all future concepts 🚀
✅ Conclusion
Classes, objects, and methods form the core of Java programming ☕
By understanding:
- How classes define blueprints
- How objects are created and stored
- How methods are invoked and executed
- How memory and references work
- Common mistakes and tricky scenarios
Java programs become structured, logical, and maintainable 💪
A solid grasp of these concepts makes advanced topics like constructors, inheritance, polymorphism, and abstraction much easier to master.