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

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.

Similar Reads

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....

Instance in Java

...

Difference between Object and 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....

Contact Us