Backend Development 8 min read

Various Ways to Create Objects in Java: new, newInstance, Reflection, Cloning, and Deserialization

This article explains the multiple techniques for creating objects in Java—including the new operator, Class.newInstance, reflection with Constructor, cloning via Cloneable, and deserialization—while illustrating the underlying bytecode instructions and providing concrete code examples for each method.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Various Ways to Create Objects in Java: new, newInstance, Reflection, Cloning, and Deserialization

In everyday life we create many objects, but in Java the term "object" has a precise technical meaning that only programmers can manipulate.

The article asks whether you know the different ways to instantiate objects in Java and proceeds to review them one by one.

Using new to Create an Object

The new keyword allocates memory on the heap, calls the class constructor, and returns a reference. Example:

Object obj = new Object();

The corresponding bytecode shows a new instruction followed by invokespecial to invoke the no‑argument constructor, and later stack manipulation instructions such as dup and astore_1 .

Using newInstance Method

The newInstance method of Class creates an object by invoking its no‑argument constructor. Two common forms are:

User user = (User)Class.forName("com.cxuan.test.User").newInstance();

// or
User user = User.class.newInstance();

The generated bytecode includes an invokestatic for Class.forName and a checkcast to convert the resulting Object to User .

Using Reflection to Create an Object

Reflection can also create objects via the Constructor class:

Constructor<User> constructor = User.class.getConstructor();
User user = constructor.newInstance();

The bytecode shows loading of the constructor reference and invocation of newInstance on it.

Using Object Cloning

If a class implements Cloneable and overrides clone() , a new instance can be produced without calling any constructor:

Constructor<User> constructor = User.class.getConstructor();
User user = constructor.newInstance();
user.setName("cxuan");
User user2 = (User)user.clone();
System.out.println(user2.getName());

The associated bytecode mainly pushes the object reference onto the stack and invokes the clone method.

Using Deserialization to Create an Object

During deserialization the JVM creates a new object without invoking its constructor:

ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("xxx"));
out.writeObject(user2);
out.close();
// Deserialization
ObjectInputStream in = new ObjectInputStream(new FileInputStream("xxx"));
User user3 = (User) in.readObject();
in.close();
user3.setName("cxuan003");
System.out.println(user3 + ", hashcode : " + user3.hashCode());

The underlying bytecode follows the same patterns discussed earlier, with no special instructions beyond standard method calls.

Overall, the article reviews five distinct object‑creation mechanisms in Java—using new , Class.newInstance , reflection via Constructor , cloning, and deserialization—illustrating each with code snippets and the relevant bytecode instructions.

JavabytecodereflectionObject CreationdeserializationCloningnewInstance
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.