Open In App

Difference Between Object and Instance in Java

Last Updated : 24 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The object is an instance of a class. A class is like a blueprint or template that defines the properties and behavior of objects. When we create an object we are creating an instance of that class.

Object in Java

The object is an instance of a class. A class is a blueprint or template that describes the behavior and properties of the objects of the class. When we create an object in Java, we create an instance of that class that has its own set of properties and can perform actions based on the behavior defined in the class.

Syntax:

ClassName objectName = new ClassName();

Below is the implementation of the above topic:

Java




// Java Program to demonstrate
// Object in Java
import java.io.*;
  
// Driver Class
public class Person {
    String name;
    int age;
  
    public void sayHello()
    {
        System.out.println("Hello, my name is " + name
                           + " and I'm " + age
                           + " years old.");
    }
  
      // main function
    public static void main(String[] args)
    {
        Person person1 = new Person();
        person1.name = "kumar";
        person1.age = 27;
        person1.sayHello();
  
        Person person2 = new Person();
        person2.name = "Bob";
        person2.age = 32;
        person2.sayHello();
    }
}


output :

Hello, my name is kumar and I'm 27 years old.
Hello, my name is Bob and I'm 32 years old.

Instance in Java

The instance is a specific occurrence of a class at runtime. It is created using the “new” keyword and represents the unique memory allocation with its own set of instance variables that store its state.

Syntax :

ClassName instanceName = new ClassName(arguments);

Below is the implementation of the above topic:

Java




import java.io.*;
public class Circle {
    int radius;
  
    public double calculateArea() {
        return Math.PI * radius * radius;
    }
  
    public static void main(String[] args) {
        Circle circle1 = new Circle();
        circle1.radius = 5;
        double area1 = circle1.calculateArea();
        System.out.println("Area of circle1 is " + area1);
  
        Circle circle2 = new Circle();
        circle2.radius = 10;
        double area2 = circle2.calculateArea();
        System.out.println("Area of circle2 is " + area2);
    }
}


Output

Area of circle1 is 78.53981633974483
Area of circle2 is 314.1592653589793

Difference between Object and Instance in Java

Characteristics

Object

Instance

Definition

object is runtime entity of a class.

instance is single occurrence of a class.

Creation

Created during runtime using new keyword.

Created when an object is instantiated using new keyword.

Memory Allocation

Occupies memory space based on class definition.

Occupies memory space based on class definition.

Purpose

Represents a specific instance of a class.

Represents a single occurrence or instantiation of a class.

Identity

Each object has a unique identity.

Each instance has a unique identity within object.

Usage

The Objects can be used to call methods and access fields.

The Instances are used to access methods and fields specific to that instance.

Example

Car my = new Car();

Person person01 = new Person();



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads