Different ways to create objects in Java Using new Keyword : Using new keyword is the most basic way to create an object. This is the most common way to create an object in java. Almost 99% of the objects are created in this way. By using this method we can call any constructor we want to call (no argument or parameterized constructors). Ex. public class NewKeywordExample { String name = 'Java'; public static void main(String[] args) { NewKeywordExample obj = new NewKeywordExample(); System.out.println(obj.name); } } Using New Instance : If we know the name of the class & if it has a public default constructor we can create an object –Class.forName. We can use it to create the Object of a Class. Class.forName actually loads the Class in Java but doesn’t create any Object. To create an Object of the Class you have to use the new Instance Method of the Class. Ex. public class NewInstanceExample { String name = 'Java'; public static void main(String[] args) { try { Class cls = Class.forName('NewInstanceExample'); NewInstanceExample obj = (NewInstanceExample) cls.newInstance(); System.out.println(obj.name); } catch (Exception e) { e.printStackTrace(); } } } Using clone() method: Whenever clone() is called on any object, the JVM actually creates a new object and copies all content of the previous object into it. Creating an object using the clone method does not invoke any constructor. To use the clone() method on an object we need to implement Cloneable and define the clone() method in it. Ex. public class CloneExample implements Cloneable { @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } String name = 'Java'; public static void main(String[] args) { CloneExample obj1 = new CloneExample(); try{ CloneExample obj2 = (CloneExample) obj1.clone(); System.out.println(obj2.name); } catch (Exception e) { e.printStackTrace(); } } } Note : *-* Here we are creating the clone of an existing Object and not any new Object. *-* Class needs to implement Cloneable Interface otherwise it will throw CloneNotSupportedException. Using deserialization : Whenever we serialize and then deserialize an object, JVM creates a separate object. In deserialization, JVM doesn’t use any constructor to create the object. To deserialize an object we need to implement the Serializable interface in the class. Ex import java.io.*; class DeserializationExample implements Serializable { private String name; DeserializationExample(String name) { this.name = name; } public static void main(String[] args) { try { DeserializationExample d = new DeserializationExample('Java'); FileOutputStream f = new FileOutputStream('file.txt'); ObjectOutputStream oos = new ObjectOutputStream(f); oos.writeObject(d); oos.close(); f.close(); } catch (Exception e) { e.printStackTrace(); } } } The object of the DeserializationExample class is serialized using the writeObject() method and written to file.txt file. Deserialization of Object : Ex import java.io.*; public class DeserializationExample { public static void main(String[] args) { try{ DeserializationExample d; FileInputStream f = new FileInputStream('file.txt'); ObjectInputStream oos = new ObjectInputStream(f); d = (DeserializationExample)oos.readObject(); } catch (Exception e) { e.printStackTrace(); } System.out.println(d.name); } } Using newInstance() method of Constructor class : This is similar to the newInstance() method of a class. There is one newInstance() method in the java.lang.reflect.Constructor class which we can use to create objects. It can also call a parameterized constructor, and private constructor by using this newInstance() method. Both newInstance() methods are known as reflective ways to create objects. In fact newInstance() method of Class internally uses newInstance() method of Constructor class. Eximport java.lang.reflect.*; public class ReflectionExample { private String name; ReflectionExample() { } public void setName(String name) { this.name = name; } public static void main(String[] args) { try { Constructor constructor = ReflectionExample.class.getDeclaredConstructor(); ReflectionExample r = constructor.newInstance(); r.setName('Java'); System.out.println(r.name); } catch (Exception e) { e.printStackTrace(); } } } - Study24x7
Social learning Network
86 followers study24x7 22 Nov 2019 03:57 PM study24x7 study24x7

Different ways to create objects in Java Using new Keyword : Using new keyword is the most basic way to create an object. This is the most common way to create an object in java. Almost 99% of the objects are created in this way. By using this method we can call any constructor ...

See more

study24x7
Write a comment
Related Questions
500+   more Questions to answer
Most Related Articles