Paramclasses Academy - Blog

Tutorials Details

What is Classes and Objects in Java with Real-Life Examples ?

What is Classes and Objects in Java with Real-Life Examples ?

October 21, 2026

Introduction

Classes and objects are the fundamental building blocks of Object-Oriented Programming (OOP) in Java. They help programmers organize data and related behavior into meaningful units.

For example, a student has a name, roll number, and marks. A student can also perform actions, such as displaying their details. In Java, we can represent this structure using a class and create individual student objects from it.

In this tutorial, you will learn what classes and objects are, how to create them, how to access their fields and methods, and how to use them in practical Java programs.

What Is a Class in Java?

A class is a blueprint or template used to define the fields and methods that its objects can have.

A class describes the structure and behavior of a type of object. It does not mean that every object created from that class must have identical data.

Real-Life Example

Imagine a house plan. The plan describes the rooms and layout, but each house built from it can have different furniture and decorations.

Similarly, a Java class defines a structure, while objects created from that class can hold their own instance data.

Syntax of a Class

class Student {
    String name;
    int rollNumber;

    void displayDetails() {
        System.out.println(name);
        System.out.println(rollNumber);
    }
}

Explanation

  • class Student declares a class named Student.

  • String name stores a student's name.

  • int rollNumber stores a student's roll number.

  • displayDetails() is a method that displays the student's information.

The class defines the structure. To use instance fields and methods, we generally create an object of the class.

What Is an Object in Java?

An object is an instance of a class. It can contain its own instance data and use the behavior defined by the class.

For example, two objects created from the Student class can represent two different students with different names and roll numbers.

Creating an Object

Java commonly uses the new keyword to create an object.

Student student1 = new Student();

This statement has three parts:

  • Student — the declared reference type.

  • student1 — a reference variable.

  • new Student() — creates a new Student object using its constructor.

The reference variable provides a way to access the object. It is not the object itself.

Difference Between Class and Object

Class Object
A blueprint defining a type An instance of a class
Declares fields and methods Has its own instance state
Declared using the class keyword Commonly created using new
Defines what instances can contain and do Represents a particular instance
Example: Student Example: a student named Aman

Easy way to remember: A class is a blueprint; an object is an individual instance built from that blueprint.

How to Create and Use Objects in Java

Let's create a simple program that uses two objects of the same class.

Java Program

class Student {
    String name;
    int rollNumber;

    void displayDetails() {
        System.out.println("Name: " + name);
        System.out.println("Roll Number: " + rollNumber);
    }
}

public class Main {
    public static void main(String[] args) {
        Student student1 = new Student();
        student1.name = "Aman";
        student1.rollNumber = 101;

        Student student2 = new Student();
        student2.name = "Priya";
        student2.rollNumber = 102;

        student1.displayDetails();
        student2.displayDetails();
    }
}

Output

Name: Aman
Roll Number: 101
Name: Priya
Roll Number: 102

Explanation

  1. The Student class defines two fields and one method.

  2. student1 refers to the first object.

  3. student2 refers to a different object.

  4. Each object stores its own name and roll number.

  5. Calling displayDetails() displays the state of the object used for the call.

This example demonstrates how one class can be used to create multiple objects with different data.

Instance Variables and Methods

A class can define fields to store data and methods to perform operations.

1. Instance Variables

Instance variables are non-static fields declared inside a class but outside its methods and constructors. Each object normally has its own copy of these fields.

Example:

class Car {
    String brand;
    String color;
}

Every Car object can have its own brand and color.

If an instance variable is not explicitly initialized, Java assigns it a default value, such as null for a reference type or 0 for an integer field.

2. Instance Methods

Instance methods are methods called on an object. They can access that object's instance variables directly.

class Car {
    String brand;

    void start() {
        System.out.println(brand + " is starting");
    }
}

To call the method:

Car car = new Car();
car.brand = "Toyota";
car.start();

Output:

Toyota is starting

What Is a Constructor in Java?

A constructor is a special class member used when creating an object to initialize it.

A constructor has the same name as its class and has no return type—not even void.

Example of a Constructor

class Student {
    String name;
    int rollNumber;

    Student(String studentName, int number) {
        name = studentName;
        rollNumber = number;
    }

    void display() {
        System.out.println(name + " - " + rollNumber);
    }
}

public class Main {
    public static void main(String[] args) {
        Student student = new Student("Aman", 101);
        student.display();
    }
}

Output:

Aman - 101

Explanation

  • Student(String studentName, int number) is a constructor.

  • The arguments supplied during object creation are used to initialize the fields.

  • new Student("Aman", 101) creates the object and invokes the constructor.

If no constructor is declared in a class, Java normally provides a default constructor. Once you declare a constructor yourself, the compiler does not automatically add that no-argument default constructor.

Constructors are useful for ensuring that objects begin with appropriate initial values.

The this Keyword in Java

The this keyword refers to the current object. It is often used when a constructor or method parameter has the same name as an instance variable.

class Student {
    String name;

    Student(String name) {
        this.name = name;
    }
}

Here:

  • this.name refers to the current object's field.

  • name refers to the constructor parameter.

Without this, the assignment could refer to the parameter on both sides rather than updating the field.

Accessing Object Members

You can access accessible fields and call methods using the dot (.) operator.

objectName.fieldName;
objectName.methodName();

For example:

Student student = new Student("Priya", 102);
student.display();

The members you can access depend on their access modifiers, such as public, private, and protected.

For good object-oriented design, fields are often declared private and exposed through carefully designed methods when needed.

Static Members vs Instance Members

Java classes can also contain static fields and methods.

Instance Members Static Members
Associated with individual objects Associated with the class
Each object generally has its own instance fields A static field is shared at the class level
Instance methods can directly access instance fields Static methods cannot directly access instance members without an object reference
Called using an object in common usage Commonly accessed using the class name

Example

class Student {
    String name;
    static String school = "ABC School";
}

public class Main {
    public static void main(String[] args) {
        Student student = new Student();
        student.name = "Aman";

        System.out.println(student.name);
        System.out.println(Student.school);
    }
}

Output:

Aman
ABC School

The name field belongs to each student object, while school is a static field shared by the class.

Practical Example: Employee Management

Let's create a simple employee program using a class, fields, a constructor, and a method.

class Employee {
    private String name;
    private double salary;

    Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    void displayDetails() {
        System.out.println("Employee: " + name);
        System.out.println("Salary: ₹" + salary);
    }
}

public class Main {
    public static void main(String[] args) {
        Employee employee1 = new Employee("Rahul", 25000);
        Employee employee2 = new Employee("Neha", 30000);

        employee1.displayDetails();
        employee2.displayDetails();
    }
}

Output

Employee: Rahul
Salary: ₹25000.0
Employee: Neha
Salary: ₹30000.0

What Does This Program Teach?

  • The Employee class defines the structure of an employee.

  • Each employee object stores its own name and salary.

  • The constructor initializes each object.

  • The private fields restrict direct access from outside the class.

  • The displayDetails() method presents the object's data.

In a real application, salary changes would normally be handled through validated methods rather than by allowing unrestricted modification.

Common Mistakes Beginners Should Avoid

  1. Forgetting to create an object: Instance methods and fields generally require an object reference.

  2. Confusing a reference with an object: A reference variable is used to access an object; it is not the object itself.

  3. Using the wrong constructor arguments: The arguments must match an available constructor.

  4. Giving a constructor a return type: A constructor has no return type.

  5. Accessing private fields directly: Use the class's intended public methods where appropriate.

  6. Confusing static and instance members: Static members belong to the class; instance members belong to individual objects.

  7. Assuming every class must be instantiated: Some classes, such as utility classes, may be designed for static methods only. Abstract classes cannot be instantiated directly.

Frequently Asked Questions (FAQs)

1. What is a class in Java?

A class is a blueprint that defines the fields and methods associated with a type of object.

2. What is an object in Java?

An object is an instance of a class that can have its own state and use the behavior defined by the class.

3. How do you create an object in Java?

A common way is to use the new keyword, followed by a constructor call. For example: Student student = new Student();

4. What is the difference between a class and an object?

A class defines the structure, while an object is an individual instance of that class.

5. What is a constructor?

A constructor is used during object creation to initialize the object. It has the same name as its class and no return type.

6. What is the purpose of the this keyword?

The this keyword refers to the current object and can distinguish instance fields from parameters with the same name.

7. What is the difference between static and instance variables?

An instance variable belongs to an individual object. A static field is associated with the class and shared at the class level.

8. Can one class have multiple objects?

Yes. A single class can be used to create many objects, each with its own instance state.

Practice Exercises for Beginners

Try these programs yourself:

  1. Create a Book class with fields for title, author, and price. Create two objects and display their details.

  2. Create a Mobile class with brand and price fields and a method that displays its information.

  3. Create a Student class with a parameterized constructor that initializes name and roll number.

  4. Create an Employee class with private fields and a method that displays employee details.

  5. Create a Counter class with a static field that counts how many objects have been created. Initialize the count correctly in the constructor.

Conclusion

Classes and objects are essential concepts in Java and form the foundation of object-oriented programming. A class defines the structure and behavior, while objects represent individual instances with their own state.

By learning fields, methods, constructors, the this keyword, and static members, you can write Java programs that are easier to organize and maintain.

Practice creating small classes such as Student, Car, Book, and Employee. Once you understand these concepts, you will be better prepared to learn constructors in greater depth, inheritance, polymorphism, and other advanced Core Java topics.