Home Java Objects and Classes

Objects and Classes in Java

Beginner ⏱ 8 min read Updated: Jun 2026

Classes and Objects in Java are the fundamental concepts of Object-Oriented Programming (OOP). A class acts as a blueprint for creating objects, while an object represents a real-world entity with properties and behaviors.

Java uses classes and objects to create reusable, modular, and organized code. Almost every Java program is built using these two important concepts.

What is a Class in Java?

A class in Java is a blueprint or template used to create objects. It defines the data members (variables) and methods (functions) that describe the behavior of an object.

A class itself does not occupy memory for instance variables until an object of that class is created.

💡
Key Point: A class defines the structure and behavior of objects.

Syntax of Class in Java

Syntax
class ClassName { 
    // variables
    // methods
}

Example of Class in Java

Example
class Student {
    String name;
    void display() { 
        System.out.println(name);
    }
    }

What is an Object in Java?

An object in Java is an instance of a class. It represents a real-world entity and contains its own state and behavior.

The state of an object is represented by variables, and its behavior is represented by methods.

State

Represents the data or properties of an object.

Behavior

Represents actions performed by an object using methods.

Creating an Object in Java

Objects in Java are created using the new keyword. The new keyword allocates memory for the object and returns its reference.

Example
class Student {
    String name;
}
public class Main { 
    public static void main(String[] args) { 
         Student s1 = new Student(); 
         s1.name = "Alex";
        System.out.println(s1.name);
    }
 }
Output
Alex

Class and Object Relationship in Java

  • A class is a blueprint used to create objects.
  • An object is an instance of a class.
  • Multiple objects can be created from a single class.
  • Each object can have different values for its properties.

Difference Between Class and Object in Java

  • Class: It is a template or blueprint for creating objects.
  • Object: It is an actual instance created from a class.
  • Class does not represent a real-world entity, but an object does.
  • Objects occupy memory, while classes do not store instance data.

Features of Classes and Objects in Java

Encapsulation

Combines data and methods into a single unit.

Reusability

Allows code reuse by creating multiple objects.

Modularity

Helps divide large programs into smaller components.

Advantages of Objects and Classes in Java

Code Reuse

Classes allow developers to reuse code efficiently.

Easy Maintenance

Object-oriented structure makes programs easier to maintain.

Real World Modeling

Helps represent real-world entities in programs.