5 Ways to Create Java Objects: From new Keyword to Deserialization
This article explains five core techniques for instantiating Java objects—including the new keyword, Class.newInstance(), Constructor.newInstance(), clone(), and deserialization—provides code examples for each method, and demonstrates their usage in a complete program with sample output.
As Java developers we often need to create objects, and beyond the usual new operator or Spring containers there are several alternative techniques.
1. Using the new keyword
The most common way, invoking any constructor (no‑arg or parameterized).
Employee emp1 = new Employee();2. Using Class newInstance()
Invoke the no‑arg constructor via reflection.
Employee emp2 = (Employee) Class.forName("org.programming.mitra.exercises.Employee").newInstance();Or the shortcut:
Employee emp2 = Employee.class.newInstance();3. Using Constructor newInstance()
Obtain a Constructor object and call its newInstance() method, which can also invoke parameterized or private constructors.
Constructor<Employee> constructor = Employee.class.getConstructor();
Employee emp3 = constructor.newInstance();4. Using clone()
When clone() is called, the JVM creates a new object without invoking any constructor. The class must implement Cloneable and define clone().
Employee emp4 = (Employee) emp3.clone();5. Using deserialization
Reading a serialized object also creates a new instance without calling a constructor; the class must implement Serializable.
ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj"));
Employee emp5 = (Employee) in.readObject();Example Employee class
class Employee implements Cloneable, Serializable {
private static final long serialVersionUID = 1L;
private String name;
public Employee() { System.out.println("Employee Constructor Called..."); }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
@Override public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return Objects.equals(name, employee.name);
}
@Override public int hashCode() { return Objects.hash(name); }
@Override public String toString() { return String.format("Employee{name='%s'}", name); }
@Override public Object clone() {
try { return super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); return null; }
}
}Complete program demonstrating all five methods
public class ObjectCreation {
public static void main(String[] args) throws Exception {
// 1. new keyword
Employee emp1 = new Employee();
emp1.setName("emp1");
// 2. Class.newInstance()
Employee emp2 = Employee.class.newInstance();
emp2.setName("emp2");
// 3. Constructor.newInstance()
Constructor<Employee> constructor = Employee.class.getConstructor();
Employee emp3 = constructor.newInstance();
emp3.setName("emp3");
// 4. clone()
Employee emp4 = (Employee) emp3.clone();
emp4.setName("emp4");
// serialize emp4
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("data.obj"))) {
out.writeObject(emp4);
}
// 5. deserialization
Employee emp5;
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj"))) {
emp5 = (Employee) in.readObject();
emp5.setName("emp5");
}
System.out.println(emp1 + ", hashcode : " + emp1.hashCode());
System.out.println(emp2 + ", hashcode : " + emp2.hashCode());
System.out.println(emp3 + ", hashcode : " + emp3.hashCode());
System.out.println(emp4 + ", hashcode : " + emp4.hashCode());
System.out.println(emp5 + ", hashcode : " + emp5.hashCode());
}
}Sample output
Employee Constructor Called...
Employee Constructor Called...
Employee Constructor Called...
Employee{name='emp1'}, hashcode : 3117192
Employee{name='emp2'}, hashcode : 3117193
Employee{name='emp3'}, hashcode : 3117194
Employee{name='emp4'}, hashcode : 3117195
Employee{name='emp5'}, hashcode : 3117196The program shows that methods 1‑3 invoke constructors (visible in the printed messages), while methods 4 and 5 create objects without calling a constructor.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
