Classes and Objects in Java

Last Updated : 24 Nov, 2026

Classes and objects are fundamental concepts of Object-Oriented Programming (OOP) in Java. They help organize data and methods into reusable structures.

  • A class defines the properties and behavior of objects.
  • An object is an instance of a class with its own state and identity.
  • Multiple objects can be created from the same class.

For example, Dog is a class, while Tommy and Tuffy are objects of that class.

e141e481-770e-46e2-8df9-82ac52bb739f
Class and Object Relationship in Java

Java Class

A class is a blueprint for creating objects. It defines fields to store data and methods to perform operations.

  • A class can contain fields, methods, constructors, and nested types.
  • Objects created from the same class share its defined structure.
Java
class Student{
    int id;
    String name;

    // Constructor to initialize fields
    Student(int id, String name){
        this.id = id;
        this.name = name;
    }
    void display(){
        System.out.println(id + " " + name);
    }
    public static void main(String[] args){
        
        // Create an object
        Student s1 = new Student(10, "Alice");
        s1.display();
    }
}

Output
10 Alice

Explanation: The Student class defines two fields and a method. The new keyword creates an object, and the constructor initializes its fields. The display() method prints their values.

Syntax:

class ClassName {
// Fields
// Constructors
// Methods
}

Java Objects

An object is an instance of a class created during program execution. It has three main characteristics:

  • State refers to the values stored in an object's instance fields.
  • Behavior refers to the operations performed through its methods.
  • Identity distinguishes an object from other objects, even when they have the same field values.
Classes-and-object

Object Declaration and Instantiation

Object creation involves declaring a reference variable, creating an object, and assigning its reference.

Declaration/Instantiation and Initialization

This declares a reference variable named tuffy. No Dog object is created at this stage. The local variable must be assigned before use.

Dog tuffy; //Declaration

The new keyword creates an object, invokes its constructor, and returns a reference to it.

tuffy = new Dog("Tuffy", 5); //Instantiation and Initialization

Ways to Create Objects

Java provides several ways to create objects. The most common is using the new keyword.

1. Using the new Keyword

The new keyword creates an instance of a class and invokes its constructor.

Student s = new Student(10, "Alice");

Here, s holds a reference to the newly created Student object.

2. Using Reflection

Reflection allows objects to be created dynamically at runtime.

Student s = Student.class.getDeclaredConstructor().newInstance();

This example requires an accessible no-argument constructor and appropriate exception handling.

3. Using the clone() Method

The clone() method can create a copy of an existing object.

Geeks g2 = (Geeks) g1.clone();

The class must support cloning, typically by implementing Cloneable and overriding clone(). The default cloning behavior creates a shallow copy.

4. Using Deserialization

Deserialization reconstructs an object from a serialized byte stream.

Student s = (Student) in.readObject();

Here, in is an ObjectInputStream. The class must support Java serialization, and the stream must contain a compatible serialized object.

Difference Between Class and Object

Class

Object

A blueprint that defines fields and methods.

An instance created from a class.

Defines structure and behavior.

Holds state and has identity.

Declared using the class keyword.

Commonly created using the new keyword.

Does not require an instance to be declared.

Requires a class or another instantiable type.

Example: Dog

Example: new Dog("Tommy")

Important Points

  • A class can be used to create multiple objects, each with its own instance field values.
  • Instance methods can access the state of the object on which they are called.
  • Java automatically provides a default constructor if a class declares no constructors.
  • Objects are generally allocated on the heap, but the JVM may optimize their allocation.
  • A reference variable can refer to an object or contain null.
Comment