What is the difference between a class and an object in Java?
Fundamental Concept
Class is a blueprint or template for creating objects, while an Object is a specific instance of a class.
Detailed Comparison
| Aspect | Class | Object |
|---|---|---|
| Nature | Template/Blueprint | Instance/Realization |
| Memory | No memory allocated when defined | Memory allocated when created |
| Declaration | Using class keyword |
Using new keyword |
| Existence | Logical entity | Physical entity |
| Quantity | One class definition | Multiple objects can be created |
Detailed Explanation with Examples
1. Class Definition (Blueprint)
// Class Definition - This is just a blueprint
public class Car {
// Fields (attributes/properties)
private String brand;
private String color;
private int year;
private double price;
// Constructor - special method to initialize objects
public Car(String brand, String color, int year, double price) {
this.brand = brand;
this.color = color;
this.year = year;
this.price = price;
}
// Methods (behaviors)
public void startEngine() {
System.out.println(brand + " engine started!");
}
public void displayInfo() {
System.out.println("Brand: " + brand + ", Color: " + color +
", Year: " + year + ", Price: $" + price);
}
// Getter and Setter methods
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
2. Object Creation (Instances)
public class Main {
public static void main(String[] args) {
// Creating objects (instances) of Car class
Car car1 = new Car("Toyota", "Red", 2022, 25000.0);
Car car2 = new Car("Honda", "Blue", 2023, 28000.0);
Car car3 = new Car("Ford", "Black", 2021, 22000.0);
// Each object has its own state and behavior
car1.startEngine(); // Output: Toyota engine started!
car2.startEngine(); // Output: Honda engine started!
car1.displayInfo(); // Output: Brand: Toyota, Color: Red, Year: 2022, Price: $25000.0
car2.displayInfo(); // Output: Brand: Honda, Color: Blue, Year: 2023, Price: $28000.0
}
}
More Comprehensive Example
Class Definition
public class Student {
// Static variable - belongs to class, not objects
private static int studentCount = 0;
// Instance variables - each object has its own copy
private int studentId;
private String name;
private int age;
private String course;
// Constructor
public Student(String name, int age, String course) {
this.studentId = ++studentCount; // Auto-increment ID
this.name = name;
this.age = age;
this.course = course;
}
// Instance methods
public void study() {
System.out.println(name + " is studying " + course);
}
public void takeExam() {
System.out.println(name + " is taking " + course + " exam");
}
// Static method - belongs to class
public static int getStudentCount() {
return studentCount;
}
// Getters
public int getStudentId() { return studentId; }
public String getName() { return name; }
public int getAge() { return age; }
public String getCourse() { return course; }
// Setters
public void setName(String name) { this.name = name; }
public void setAge(int age) { this.age = age; }
public void setCourse(String course) { this.course = course; }
@Override
public String toString() {
return "Student[ID: " + studentId + ", Name: " + name +
", Age: " + age + ", Course: " + course + "]";
}
}
Object Usage
public class SchoolManagement {
public static void main(String[] args) {
// Creating multiple objects from the same class
Student student1 = new Student("Alice Johnson", 20, "Computer Science");
Student student2 = new Student("Bob Smith", 22, "Mathematics");
Student student3 = new Student("Carol Davis", 21, "Physics");
// Each object maintains its own state
System.out.println(student1); // Student[ID: 1, Name: Alice Johnson, Age: 20, Course: Computer Science]
System.out.println(student2); // Student[ID: 2, Name: Bob Smith, Age: 22, Course: Mathematics]
System.out.println(student3); // Student[ID: 3, Name: Carol Davis, Age: 21, Course: Physics]
// Objects can perform actions
student1.study(); // Alice Johnson is studying Computer Science
student2.takeExam(); // Bob Smith is taking Mathematics exam
// Accessing static method through class name
System.out.println("Total students: " + Student.getStudentCount()); // Total students: 3
// Modifying object state
student1.setCourse("Software Engineering");
System.out.println("Updated: " + student1); // Student[ID: 1, Name: Alice Johnson, Age: 20, Course: Software Engineering]
// Demonstrating that objects are independent
System.out.println("\n--- Independent Objects ---");
System.out.println("Student1 ID: " + student1.getStudentId());
System.out.println("Student2 ID: " + student2.getStudentId());
System.out.println("Student3 ID: " + student3.getStudentId());
}
}
Key Differences in Detail
1. Declaration and Usage
// CLASS DECLARATION (no memory allocation)
class BankAccount {
private String accountNumber;
private double balance;
public BankAccount(String accountNumber, double initialBalance) {
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
public void deposit(double amount) {
balance += amount;
}
public double getBalance() {
return balance;
}
}
// OBJECT CREATION (memory allocated)
public class BankDemo {
public static void main(String[] args) {
// Creating objects - memory allocated for each
BankAccount account1 = new BankAccount("ACC001", 1000.0);
BankAccount account2 = new BankAccount("ACC002", 5000.0);
// Each object operates independently
account1.deposit(500.0);
account2.deposit(1000.0);
System.out.println("Account1 Balance: $" + account1.getBalance()); // $1500.0
System.out.println("Account2 Balance: $" + account2.getBalance()); // $6000.0
}
}
2. Memory Allocation Example
class MemoryDemo {
private int data;
public MemoryDemo(int data) {
this.data = data;
}
public int getData() {
return data;
}
}
public class MemoryExample {
public static void main(String[] args) {
// Each 'new' keyword allocates new memory
MemoryDemo obj1 = new MemoryDemo(10); // Memory allocated for obj1
MemoryDemo obj2 = new MemoryDemo(20); // Memory allocated for obj2
MemoryDemo obj3 = new MemoryDemo(30); // Memory allocated for obj3
System.out.println("obj1 data: " + obj1.getData()); // 10
System.out.println("obj2 data: " + obj2.getData()); // 20
System.out.println("obj3 data: " + obj3.getData()); // 30
// obj1, obj2, obj3 are different objects in memory
// They don't share the 'data' field
}
}
Real-World Analogy
| Programming Concept | Real-World Equivalent |
|---|---|
| Class | Architectural blueprint for a house |
| Object | Actual house built using that blueprint |
| Multiple Objects | Multiple houses built from same blueprint |
Summary of Important Points
- Class is a template, Object is an instance
- Class defines structure, Object contains actual data
- One class can create many objects
- Objects are independent - changes to one don't affect others
- Memory is allocated only when objects are created using
new - Class exists at compile time, Objects exist at runtime
0
likes
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
