What does instantiate mean in Java?

Instantiate means to create an object of a class using a new keyword. The new operator instantiates a class by allocating memory for a new object. The new operator also invokes the object constructor. Any number of instances can be created for one class.

The new operator requires a single, postfix constructor call. The name of the constructor gives us the name of the class to instantiate. The new operator creates the object, the constructor initializes it.

A class contains variables and methods. These variables and methods are called member variables and member methods. To reference the desired member variable or method of the instance we can use the dot operator.

By using a constructor, one can create instances with certain predefined values. The constructor has the same name as the class without a return statement.

class Triangle {
  public int base = 10;
  public int height = 10;
  public Triangle(int b, int h) {
    System.out.println("Constructor with arguments");
    this.base = b;
    this.height = h;
  }
  public void printData() {
    System.out.println("Base :" + base + " Height :" + height);
  }
}
public class Example {
  public static void main(String args[]) {
    // an instance of the class Triangle is created
    Triangle tri = new Triangle(30, 10);
    tri.printData();
  }
}

The output is

Constructor with arguments
Base :30 Height :10

Instantiating using newInstance()

The Class.newInstance() can be used to create an object. It creates a new instance of the class represented by this Class object.

The syntax is

Class_name Object_name = Class_name.class.newInstance();  

The demonstration is shown in the program

public class Demo {
  String str = "Welcome user";
  public static void main(String args[]) {
    try {
      //creating object of class Demo
      Demo obj = Demo.class.newInstance();
      System.out.println(obj.str);
    } catch (Exception e) {
      System.out.println(e); 
    }
  }
}

The output is

Welcome user

Instantiating using Class.forName()

When the name of the class is known and if it has a default constructor an object can be created using Class.forName.

The syntax is

Class_name Object_name = (Class_name)Class.forName("Class_name").newInstance();

The demonstration is shown in the program

public class Example {
  String str = "Instantiating";
  public static void main(String args[]) {
    try {
      //creating object of class Example
      Example obj = (Example) Class.forName("Example").newInstance();
      System.out.println(obj.str);
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}

The output is

Instantiating

Instantiating using newInstance() method of Constructor class

The Constructor class has a newInstance() method. The newInstance() method belongs to the java.lang.reflect.Constructor class.

The syntax is

Class_name object_name = Class_name.class.getConstructor().newInstance();

The demonstration is shown below.

import java.lang.reflect.Constructor;
public class Example {
  String str = "Full Stack Developer";
  public static void main(String args[]) {
    try {
      Example ex = Example.class.getConstructor().newInstance();
      System.out.println(ex.str);
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}

The output is

Full Stack Developer

Instantiating using clone method

In Java when clone() method can be used to create objects. When we create an object using clone() method, the constructor is not invoked. To use clone() method, one should implement Cloneable interface.

The syntax is

Class_name Object1_name = Object2_name.clone();

The demonstration is shown below.

public class Example implements Cloneable {
  //creates and returns a copy of this object  
  protected Object clone() throws CloneNotSupportedException {
    return super.clone();
  }
  String name = "Java Programmer";
  public static void main(String[] args) {
    Example obj1 = new Example(); //creating object of class  
    try {
      Example obj2 = (Example) obj1.clone();
      System.out.println(obj1.name);
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}

The output is

Java Programmer
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions