Different ways to create an object in java?


In Java a class is a user defined datatype/blue print in which we declare methods and variables.

public class Sample{
}

Once you declare a class you need to create an object (instantiate) it. You can create an object using various ways −

Using new keyword

In general, an object is created using the new keyword as −

Sample obj = new Sample();

Example

In the following Java we have a class with name sample, which has a method (display). From the main method we are instantiating the Sample class and invoking the display() method.

 Live Demo

public class Sample{
   public void display() {
      System.out.println("This is the display method of the Sample class");
   }
   public static void main(String[] args){
      //Instantiating the Sample class
      Sample obj = new Sample();
      obj.display();
   }
}

Output

This is the display method of the Sample class

Using the newInstance() method

The newInstance() method of the class named Class creates an object of the class represented by this Class object.

You can get the Class object of a class by passing its name as String to the forName() method.

Sample obj2 = (Sample) Class.forName("Sample").newInstance();

Example

In the following Java we have a class with name sample, which has a method (display). From the main method we are creating an object of the Sample class using the newInstance() method of the class Class.

 Live Demo

public class Sample{
   public void display() {
      System.out.println("This is the display method of the Sample class");
   }
   public static void main(String[] args) throws Exception{
      //Creating the Class object of Sample class
      Class cls = Class.forName("Sample");
      Sample obj = (Sample) cls.newInstance();
      obj.display();
   }
}

Output

This is the display method of the Sample class

Using the clone() method

The clone() of the Object class creates and returns an object of the current class.

Example

In the following Java we have a class with name sample, which has a method (display). From the main method we are creating an object of the Sample class and from it we are creating another object using the clone() method.

public class Sample{
   public void display() {
      System.out.println("This is the display method of the Sample class");
   }
   public static void main(String[] args) throws Exception{
      //Creating the Class object of Sample class
      Sample obj1 = new Sample();
      //Creating another object using the clone() method
      Sample obj2 = (Sample) obj1.clone();
      obj2.display();
   }
}

Output

This is the display method of the Sample class

Using the constructor class from lang.reflect

The newInstance() method of the java.lang.reflect.Constructor class creates and returns a new object using the current constructor.

Example

In the following Java we have a class with name sample, which has a method (display). From the main method we are creating an object of the Sample class using the java.lang.reflect.Constructor class.

 Live Demo

import java.lang.reflect.Constructor;
public class Sample {
   public void display() {
      System.out.println("This is the display method of the Sample class");
   }
   public static void main(String[] args) throws Exception{
      //Creating a Constructor class
      Constructor constructor = Sample.class.getDeclaredConstructor();
      //Creating an object using the newInstance() method
      Sample obj = constructor.newInstance();
      obj.display();
   }
}

Output

This is the display method of the Sample class

Updated on: 29-Jun-2020

542 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements