Home »
Java programming language
Different ways to create an object in Java
Ways to create an object: Here, we are going to learn what are the different ways to create an object of a class in Java?
By Preeti Jain Last updated : January 14, 2024
Objects creation of a class in Java
There are five different ways to create an object and we will see the ways to create an object given below:
- Using new keyword
- Using newInstance() method of Class
- Using clone() method
- Using newInstance() method of Constructor class
- Using deserialization
1) Using new keyword
- new is a keyword which is introduced in Java.
- We mostly use new keyword to create an object.
- By using new keyword we can call any constructor we want to call.
Java program to create an object using new keyword
class CreateObjectByNew {
// Default Constructor
CreateObjectByNew() {
System.out.println("We are creating an object by using new keyword");
}
}
class Main {
public static void main(String[] args) {
// Creating an object of CreateObjectByNew class
CreateObjectByNew cobn = new CreateObjectByNew();
}
}
Output
E:\Programs>javac Main.java
E:\Programs>java Main
We are creating an object by using new keyword
2) Using newInstance() method of Class
- This newInstance() method is available in Class.
- By using newInstance() method of Class it can call the no-argument constructor or default constructor.
- It creates a new instance of the class.
Java program to create an object using newInstance() method of Class
class NewInstanceMethodOfClass {
// Default constructor
NewInstanceMethodOfClass() {
System.out.println("Object by using newInstance() method of Class");
}
}
class Main {
public static void main(String[] args) throws Exception {
// Creating an object of Class class and
// we are passing our class as an argument
// in forName() method of Class
Class cl = Class.forName("NewInstanceMethodOfClass");
// Now we are calling newInstance() method of Class
// and returns a reference of our created class
NewInstanceMethodOfClass nimoc = (NewInstanceMethodOfClass) cl.newInstance();
}
}
Output
E:\Programs>javac Main.java
E:\Programs>java Main
Object by using newInstance() method of Class
3) Using clone() method
- This method is available in java.lang.Cloneable interface.
- We have override clone() method of Cloneable interface in our class.
- It is the easiest way of copying an object.
Java program to create an object using clone() method
class CreateObjectByClone implements Cloneable {
String name;
CreateObjectByClone(String name) {
this.name = name;
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
public static void main(String[] args) throws Exception {
CreateObjectByClone cobc1 = new CreateObjectByClone("Preeti");
CreateObjectByClone cobc2 = (CreateObjectByClone) cobc1.clone();
System.out.println("The values before clone() in cobc1" + " " + cobc1.name);
System.out.println("The values after clone() in cobc2" + " " + cobc2.name);
}
}
Output
E:\Programs>javac CreateObjectByClone.java
E:\Programs>java CreateObjectByClone
The values before clone() in cobc1 Preeti
The values after clone() in cobc2 Preeti
4) Using newInstance() method of Constructor class
- It also creates a new instance of a class similar to the newInstance() method of the Class class.
- By using newInstance() method of Constructor class it can call any number of argument constructor.
Java program to create an object using newInstance() method of Constructor class
import java.lang.reflect.Constructor;
class NewInstanceMethodOfConstructor {
String fname, lname;
// passName() method with two argument
public void passName(String fname, String lname) {
this.fname = fname;
this.lname = lname;
}
}
class Main {
public static void main(String[] args) {
try {
// calling constructor and returns a reference of Constructor class
Constructor cons = NewInstanceMethodOfConstructor.class.getDeclaredConstructor();
// Now we are calling newInstance() method of Class
// and returns a reference of our created class
NewInstanceMethodOfConstructor nimoc = (NewInstanceMethodOfConstructor) cons.newInstance();
nimoc.passName("Preeti", "Jain");
System.out.println("My full name is :" + nimoc.fname + " " + nimoc.lname);
} catch (NoSuchMethodException ex) {
System.out.println(ex.getMessage());
} catch (SecurityException ex) {
System.out.println(ex.getMessage());
} catch (InstantiationException ex) {
System.out.println(ex.getMessage());
} catch (IllegalAccessException ex) {
System.out.println(ex.getMessage());
} catch (IllegalArgumentException ex) {
System.out.println(ex.getMessage());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
Output
E:\Programs>javac Main.java
E:\Programs>java Main
My full name is :Preeti Jain
5) Using deserialization
- When we serialize an object then after we deserialize it so the run time environment JVM create a separate object for that.
- In the case of deserialization, it does not call any constructor to create an object.
- Our class must implements Serializable interface.
- We will use two classes named ObjectOutputStream and ObjectInputStream and their two methods writeObject() of ObjectOutputStream and readObject() of ObjectInputStream.
- writeObject(): This method serialize an object and transfer it to the output stream.
- readObject(): This method references an object out of the stream and then deserialize it.
Java program to create an object using deserialization
// Java program to demonstrating serialize and deserializing an object
import java.io.*;
class Serialize implements Serializable {
public String fname, lname;
public Serialize(String fname, String lname) {
this.fname = fname;
this.lname = lname;
}
}
public class Deserialization {
public static void main(String[] args) {
Serialize serialize = new Serialize("Ronit", "Jain");
try {
FileOutputStream fos = new FileOutputStream("E:\\Programs\\myjava.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(serialize);
oos.close();
fos.close();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
serialize = null;
try {
FileInputStream fis = new FileInputStream("E:\\Programs\\myjava.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
serialize = (Serialize) ois.readObject();
ois.close();
fis.close();
System.out.println("His full name is :" + serialize.fname + " " + serialize.lname);
} catch (IOException ex) {
System.out.println(ex.getMessage());
} catch (ClassNotFoundException ex) {
System.out.println(ex.getMessage());
}
}
}
Output
E:\Programs>javac Deserialization.java
E:\Programs>java Deserialization
His full name is :Ronit Jain