100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java

Classes and Objects in Java

Learn how classes act as blueprints and objects as instances in Java, with syntax, examples, and exam-focused explanations.

OOP BasicsBeginner9 min readJul 7, 2026
Analogies

1. Introduction

Java is an object-oriented programming (OOP) language, and the class is the fundamental building block of every Java program. A class is a blueprint or template that defines the structure (fields) and behavior (methods) that its objects will have. An object is a concrete instance of a class, created at runtime, with its own copy of state stored in memory.

🏏

Cricket analogy: A batting technique manual is like a class defining stance and grip, while Virat Kohli stepping onto the crease to bat is the object—a real instance applying that blueprint with his own bat and score.

Understanding classes and objects is essential because almost everything in Java — from simple data holders to complex frameworks — is built using this concept. A single class can be used to create many objects, each with independent field values but sharing the same defined behavior.

🏏

Cricket analogy: The 'fast bowler' class in a coaching manual is used to produce Jasprit Bumrah, Mitchell Starc, and Trent Boult—each bowler has different pace and accuracy stats but they all share the same bowling action methods.

2. Syntax

java
class ClassName {
    // fields (instance variables)
    dataType fieldName;

    // methods
    returnType methodName(parameters) {
        // method body
    }
}

// Creating an object
ClassName objectName = new ClassName();

3. Explanation

A class does not occupy memory for its fields until an object is created; the class merely describes what fields and methods every object of that type will have. The new keyword allocates memory on the heap for the object and invokes a constructor to initialize it. Each object has its own copy of instance fields, but all objects share the same method code defined in the class.

🏏

Cricket analogy: A net-practice drill sheet describing cover-drive footwork takes no space on the field until a batsman like Rohit Sharma actually steps up—only then does the ground 'allocate' his stance, while the drill's technique stays the same for everyone.

Exam tip: A class is a logical construct (no memory allocated for instance fields at compile time); an object is a physical/runtime entity created using new, occupying heap memory.

Common trap: Students often confuse 'class variables' (static) with 'instance variables' (non-static). Only static fields are shared across all objects — instance fields are unique per object.

4. Example

java
public class Student {
    // fields
    String name;
    int age;

    // method
    void display() {
        System.out.println("Name: " + name + ", Age: " + age);
    }

    public static void main(String[] args) {
        // creating two objects of Student
        Student s1 = new Student();
        s1.name = "Asha";
        s1.age = 21;

        Student s2 = new Student();
        s2.name = "Ravi";
        s2.age = 23;

        s1.display();
        s2.display();
    }
}

5. Output

text
Name: Asha, Age: 21
Name: Ravi, Age: 23

6. Key Takeaways

  • A class is a blueprint; an object is a runtime instance of that class.
  • Each object has its own copy of instance fields; methods are shared across objects.
  • The new keyword allocates heap memory and triggers constructor execution.
  • Multiple objects can be created from a single class, each independently modifiable.
  • No memory is reserved for instance fields until an object is instantiated.

Practice what you learned

Was this page helpful?

Topics covered

#Java#JavaProgrammingStudyNotes#Programming#ClassesAndObjectsInJava#Classes#Objects#Syntax#Explanation#OOP#StudyNotes#SkillVeris