See the Tutorial List

Different ways to create objects in Java

Following are the ways by which an object can be created in Java:

1) new Keyword

In Java, creating objects using new keyword is very popular and common. Using this method user or system defined default constructor is called.

Example:

	
public class NewKeyword
{ 
    String s = "studytonight"; 
    public static void main(String as[])  
    { 
NewKeyword a = new NewKeyword(); 
System.out.println(a.s); 
    } 
}

new-keyword-example

2) New Instance

In Java, if the name of the class and it has a public default constructor then we can create an object of Class.forName. the Class.forName does create an object but loads the class in java.

Example:

	
public class NewInstance
{ 
    String a = "studytonight"; 
    public static void main(String[] args) 
    { 
        try
        { 
            Class b = Class.forName("NewInstance"); 
NewInstance c = (NewInstance) b.newInstance(); 
System.out.println(c.a); 
        } 
        catch (ClassNotFoundException e) 
        { 
e.printStackTrace(); 
        } 
        catch (InstantiationException e) 
        { 
e.printStackTrace(); 
        } 
        catch (IllegalAccessException e) 
        { 
e.printStackTrace(); 
        } 
    } 
}
	

New Instance Image

3) Clone() method

In Java, clone() is called on an object. When a clone() method is called JVM creates a new object and then copy all the content of the old object into it. When an object is created using the clone() method, a constructor is not invoked. To use the clone() method in a program the class implements the cloneable and then define the clone() method.

Example:

 
	
public class CloneEg implements Cloneable
{ 
    @Override
    protected Object clone() throws CloneNotSupportedException
    { 
        return super.clone(); 
    } 
    String s = "studytonight"; 

    public static void main(String[] args) 
    { 
CloneEg a= new CloneEg(); 
        try
        { 
CloneEg b = (CloneEg) a.clone(); 
System.out.println(b.s); 
        } 
        catch (CloneNotSupportedException e) 
        { 
e.printStackTrace(); 
        } 
    } 
}
	

cloneEg program example

4) deserialization

In Java, when an object is serialized and then deserialized, JVM create another separate object. When deserialization is performed JVM does not use any constructor for creating an object.

Example:

		
import java.io.*; 

class DeserializationEg implements Serializable 
{ 
    private String a; 
DeserializationEg(String name) 
    { 
this.a = a; 
    } 

    public static void main(String[] args) 
    { 
        try
        { 
DeserializationEg b = new DeserializationEg("studytonight"); 
FileOutputStream c = new FileOutputStream("CoreJava.txt"); 
ObjectOutputStream  d = new ObjectOutputStream(c); 
d.writeObject(b); 
d.close(); 
d.close(); 
        } 
        catch (Exception e) 
        { 
e.printStackTrace(); 
        } 
    } 
}

		
	

deserialization example

deserialization main

newInstance() method of Constructor class

In Java, Under java.lang.reflect.Constructor class there is newInstance() method. It used for creating an object. This method is also called a parameterized constructor.

Example:

		
import java.lang.reflect.*;

public class ReflectionEg
{
private String s;
ReflectionEg()
{
}
public void setName(String s)
{
this.s = s;
}
public static void main(String[] args)
{
try
{
Constructorconstructor = ReflectionEg.class.getDeclaredConstructor();
ReflectionEg r = constructor.newInstance();
r.setName("studytonight");
System.out.println(r.s);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
		
	

reflectaion eg Image