# Six Ways to Create Objects in Java

# 1 Introduction

Java is an OOP program language, we need to create Java Object whenever we use it. There are six ways to create the Object.

# 2 Six ways

(1) the new keyword

Pumpkin p1 = new Pumpkin();

(2) Reflection: class newInstance()

Pumpkin p2 = Pumpkin.class.newInstance();

(3) Reflection: DeclaredConstructor newInstance()

Pumpkin p3 = Pumpkin.class.getDeclaredConstructor().newInstance();

(4) Object clone()

Pumpkin p4 = (Pumpkin) p1.clone();

Note: Object clone() method is protected, we can Override it as public. Be careful of deep-copy or not.

(5) Serializable

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object.bin"));
oos.writeObject(p1);
oos.close();

ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.bin"));
Pumpkin p5 = (Pumpkin) ois.readObject();
ois.close();

Need to implement the Serializable interface;

We can control which fields can be serialized;

serialVersionUID usage;

Differences from Externalizable;

(6) Unsafe

Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
Unsafe unsafe = (Unsafe) f.get(null);
Pumpkin p6 = (Pumpkin) unsafe.allocateInstance(Pumpkin.class);

# 3 Sample Code

The sample code as below:

package com.pkslow.basic;


import sun.misc.Unsafe;

import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class CreateObject {

    public static class Pumpkin implements Cloneable, Serializable {
        public Pumpkin(){
            System.out.println("Constructor called");
        }
        @Override
        public Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }

    public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, CloneNotSupportedException, IOException, ClassNotFoundException, NoSuchFieldException {

        System.out.println("---start---");
        System.out.println("(1) new");
        Pumpkin p1 = new Pumpkin();

        System.out.println("(2) Class newInstance");
        Pumpkin p2 = Pumpkin.class.newInstance();

        System.out.println("(3) Constructor newInstance");
        Pumpkin p3 = Pumpkin.class.getDeclaredConstructor().newInstance();

        System.out.println("(4) clone");
        Pumpkin p4 = (Pumpkin) p1.clone();

        System.out.println("(5)Serialization");
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object.bin"));
        oos.writeObject(p1);
        oos.close();

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.bin"));
        Pumpkin p5 = (Pumpkin) ois.readObject();
        ois.close();

        System.out.println("(6) Unsafe");
        Field f = Unsafe.class.getDeclaredField("theUnsafe");
        f.setAccessible(true);
        Unsafe unsafe = (Unsafe) f.get(null);
        Pumpkin p6 = (Pumpkin) unsafe.allocateInstance(Pumpkin.class);

        System.out.println("---end---");
    }
}

Output:

---start---
(1) new
Constructor called
(2) Class newInstance
Constructor called
(3) Constructor newInstance
Constructor called
(4) clone
(5)Serialization
(6) Unsafe
---end---

Called Constructor:

  • (1) the new keyword
  • (2) Reflection: class newInstance()
  • (3) Reflection: DeclaredConstructor newInstance()

Not called Constructor:

  • (4) Object clone()
  • (5) Serializable
  • (6) Unsafe
Last Updated: 8/18/2023, 11:39:36 PM