Differences Between Class and Object in Java

1. Overview

In this show tutorial, we’ll look at the difference between class and object in Java.

2. What is a Class?

A class represents a basic building block in all object-oriented programming languages. In Java programming language, every code we write should be inside a class.

The main purpose of having classes is so we can create objects from them.

We can imagine classes as blueprints or templates for object creation. In other words, the class describes characteristics an object will have (once we create it) and what actions could we perform with it.

Within a class, we usually define an object’s characteristics as instance variables and behavior as methods.

For instance, suppose we’d like to create and use objects representing a dog. We’d like to be able more than one dog, each having different characteristics.

Firstly, let’s define a class. When defining a class, we should think of the characteristics and behaviors an object will have:

Let’s consider the following class diagram:

Each dog will have characteristics: name, type, age, and color. Furthermore, each dog could perform actions like barking, sleeping, eating, and playing.

Secondly, based on the diagram, let’s define a Dog class:

public class Dog { 
    private String name; 
    private String type; 
    private double age; 
    private String color;

    public void makeSound(){ 
    public void sleep(){ 
        System.out.println("I'm sleeping"); 
    public void play(){ 
        System.out.println("Playing is super fun"); 
    public void eat(){ 
        System.out.println("Om nom nom"); 
    // getters and setters

Finally, the Dog class represents a template for every object of type Dog we create in the application. Each dog has the same characteristics (name, type, age…), but with different values.

3. What is an Object?

Simply put, an object is an instance of a class. While the class represents a logical entity, an object represents a physical entity. In other words, we use a logical entity (class) to create a physical entity (object).

Let’s create objects using our Dog class:

Dog huskey = new Dog("Tim", "Huskey", 14, "black");
Dog chihuahua = new Dog("Lisa", "Chihuahua", 3, "white");

Here, we created two instances from the Dog class. Each object has a different set of characteristics. We can create one or more objects using the same class.

Furthermore, once created, objects are stored and live inside a memory.

4. Differences Between Class And Object

class vs objects

In the following table, we can see the differences between classes and objects in Java:

Class Object
Blueprint for object creation. An instance of a class.
Describes which characteristics and actions the object will have. Has characteristics and actions defined within the class.
Logical entity. Physical entity.
Doesn’t allocate memory. Allocates memory when created.
Each class is created only once in an application. We can create more than one object using the same class.

5. Conclusion

In this short tutorial, we’ve looked at the differences between classes and objects in Java.

As always, the entire code for the examples can be found over on GitHub.