Ways to Create Objects in Java
You'll write new thousands of times, but it isn't the only way an object comes into existence. Frameworks such as Spring create objects for you without a single new in your code; copying, saving and loading objects create them too. "In how many ways can you create an object in Java?" is a favorite interview question, and knowing the answer also explains what those frameworks do behind the scenes.
Five Ways
| # | Way | Example | Runs the class's constructor? |
|---|---|---|---|
| 1 | new | new Ticket("Navika", 23) | Yes |
| 2 | Factory method | Ticket.of("Navika", 23), List.of(...) | Yes (it calls new inside) |
| 3 | Reflection | Ticket.class.getDeclaredConstructor().newInstance() | Yes |
| 4 | clone() | original.clone() | No: copies the fields |
| 5 | Deserialization | objectInputStream.readObject() | No (for a Serializable class) |
All Five in One Program
import java.io.*;
public class Ways {
static class Ticket implements Cloneable, Serializable {
String name;
int berth;
Ticket() { this("Unassigned", 0); }
Ticket(String name, int berth) { this.name = name; this.berth = berth; }
static Ticket of(String n, int b) { return new Ticket(n, b); }
@Override
public Ticket clone() throws CloneNotSupportedException {
return (Ticket) super.clone();
}
public String toString() { return name + "/" + berth; }
}
public static void main(String[] args) throws Exception {
Ticket t1 = new Ticket("Navika", 23);
Ticket t2 = Ticket.of("Srihitha", 24);
Ticket t3 = Ticket.class.getDeclaredConstructor().newInstance();
Ticket t4 = t1.clone();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try (ObjectOutputStream out = new ObjectOutputStream(bytes)) {
out.writeObject(t1);
}
Ticket t5;
try (ObjectInputStream in = new ObjectInputStream(
new ByteArrayInputStream(bytes.toByteArray()))) {
t5 = (Ticket) in.readObject();
}
System.out.println(t1 + " " + t2 + " " + t3 + " " + t4 + " " + t5);
System.out.println("t1 == t4: " + (t1 == t4)
+ ", t1 == t5: " + (t1 == t5));
}
}Navika/23 Srihitha/24 Unassigned/0 Navika/23 Navika/23
t1 == t4: false, t1 == t5: falseHow it works
- Line 22:
newcalls the two-argument constructor. - Line 23: the static factory method
ofcallsnewfor us. Factory methods can have meaningful names, reuse objects, or return a subclass. - Line 24: reflection finds the no-arg constructor at runtime and calls it. Frameworks use this to create objects from class names in configuration.
- Line 25:
clone()copies t1's fields into a new object, without running a constructor. The class must implementCloneable. - Lines 27 to 35: serialization writes t1 to bytes, and deserialization rebuilds a new object from them. The class must implement
Serializable. - The last line shows t4 and t5 are new objects (
==isfalse) with the same values.
The throws Exception on main, and the try (...) blocks, handle the errors these methods can report; A later lesson explains them.
Try it yourself: add System.out.println("constructor"); inside the two-argument constructor. How many times does it print, and for which objects?
Which to Use
| Situation | Use |
|---|---|
| Everyday code | new |
| A meaningful name, caching, or choosing a subclass | A static factory method (of, valueOf, from) |
| Frameworks, plugins, tools that create objects from class names | Reflection (usually done for you by Spring and similar) |
| Copying an existing object | A copy constructor (see "Copy Constructor") rather than clone() |
| Saving and loading objects | Serialization (covered later in the course), or more often JSON in modern apps |
Common Mistakes
Saying there's only one way. new is the common one; interviewers expect the other four.
Calling clone() without implementing Cloneable. Object.clone() throws CloneNotSupportedException.
Assuming constructor validation always runs. Clones and deserialized objects skip it; that matters for security-sensitive classes.
Interview Corner
What are the different ways to create an object in Java?
Using new; a static factory method (which uses new inside); reflection, with getDeclaredConstructor().newInstance(); clone() on an existing Cloneable object; and deserialization with ObjectInputStream.readObject(). The first three run a constructor; clone() and deserialization create the object without running the class's constructor.
Does clone() call the constructor?
No. Object.clone() allocates a new object and copies the field values directly, so no constructor of the class runs.
Practice
- Easy: Add a static factory method
vip(String name)toTicketthat always gives berth 1. - Harder: Add a counter that the constructors increase, create t1 to t5 as above, and print the counter. Explain the number.
Key Takeaways
Was this tutorial helpful?