Javatpoint Logo
Javatpoint Logo

How to Create Object in Java

The object is a basic building block of an OOPs language. In Java, we cannot execute any program without creating an object. There is various way to create an object in Java that we will discuss in this section, and also learn how to create an object in Java.

Java provides five ways to create an object.

  • Using new Keyword
  • Using clone() method
  • Using newInstance() method of the Class class
  • Using newInstance() method of the Constructor class
  • Using Deserialization

Using new Keyword

Using the new keyword is the most popular way to create an object or instance of the class. When we create an instance of the class by using the new keyword, it allocates memory (heap) for the newly created object and also returns the reference of that object to that memory. The new keyword is also used to create an array. The syntax for creating an object is:

Let's create a program that uses new keyword to create an object.

CreateObjectExample1.java

Output:

Welcome to javaTpoint

By using the new keyword, we can also invoke the constructor (default or parametrized) of the class.

CreateObjectExample2.java

Output:

Welcome to javaTpoint

Using clone() Method

The clone() method is the method of Object class. It creates a copy of an object and returns the same copy. The JVM creates a new object when the clone() method is invoked. It copies all the content of the previously created object into new one object. Note that it does not call any constructor. We must implement the Cloneable interface while using the clone() method. The method throws CloneNotSupportedException exception if the object's class does not support the Cloneable interface. The subclasses that override the clone() method can throw an exception if an instance cannot be cloned.

Note: The method creates a copy of the object not a new object.

Syntax:

We use the following statement to create a new object.

CreateObjectExample3.java

Output:

New Object Created	

Using newInstance() Method of Class class

The newInstance() method of the Class class is also used to create an object. It calls the default constructor to create the object. It returns a newly created instance of the class represented by the object. It internally uses the newInstance() method of the Constructor class.

Syntax:

It throws the IllegalAccessException, InstantiationException, ExceptionInInitializerError exceptions.

We can create an object in the following ways:

Or

In the above statement, forName() is a static method of Class class. It parses a parameter className of type String. It returns the object for the class with the fully qualified name. It loads the class but does not create any object. It throws ClassNotFoundException if the class cannot be loaded and LinkageError if the linkage fails.

To create the object, we use the newInstance() method of the Class class. It works only when we know the name of the class and the class has a public default constructor.

In the following program, we have creates a new object using the newInstance() method.

CreateObjectExample4.java

Output:

A new object created.

Using newInstance() Method of Constructor class

It is similar to the newInstance() method of the Class class. It is known as a reflective way to create objects. The method is defined in the Constructor class which is the class of java.lang.reflect package. We can also call the parameterized constructor and private constructor by using the newInstance() method. It is widely preferred in comparison to newInstance() method of the Class class.

Syntax:

The method parses an array of Objects as an argument. The values of primitive types wrapped in a wrapper Object of the appropriate type. It returns a new object created by calling the constructor. It throws IllegalAccessException, IllegalArgumentException, InstantiationException, InvocationTargetException, ExceptionInInitializerError Exceptions.

We can create an object in the following way:

Let's create a program that creates an object using the newInstance() method.

CreateObjectExample5.java

Output:

JavaTpoint

Using Deserialization

In Java, serialization is the process of converting an object into a sequence of byte-stream. The reverse process (byte-stream to object) of serialization is called deserialization. The JVM creates a new object when we serialize or deserialize an object. It does not use constructor to create an object. While using deserialization, the Serializable interface (marker interface) must be implemented in the class.

How to Create Object in Java

Serialization: The writeObject() method of the ObjectOutputStream class is used to serialize an object. It sends the object to the output stream.

Syntax:

Deserialization: The method readObject() of ObjectInputStream class is used to deserialize an object. It references objects out of a stream.

Syntax:

Note: Make the filed static or transient if we do not want to include a field as a part of the object. It will not include in the serialization process.

Let's understand the serialization and deserialization through a program.

Employee.java

We have created a class named Employee whose object is to be serialized and deserialized.

Serialization of Java Object:

In the following program, we have serialized an object of Employee class by using the writeObject() method of the ObjectOutputStream class. The state of the object is saved in the employee.txt file.

SerializationExample.java

Output:

Successfully Created

Deserialization of Java Object:

In the following program, we going to deserialize an object that we have serialized in the above program.

DeserializationExample.java

Output:

198054 Andrew

In the above five methods, we have noticed that the new keyword and both newInstance() methods use the constructor to create objects, while the rest two methods do not use the constructor.


Next TopicJava Tutorial





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA