Six Ways to Create Objects in Java
This article presents six Java object‑creation techniques—including direct instantiation, cloning, reflection‑based allocation, dynamic class loading, constructor invocation via reflection, and deserialization—explaining each method with code examples and practical notes for developers.
Background: The author wishes programmers a happy Valentine’s Day and introduces six ways to create objects in Java, ranging from the simplest to more advanced techniques.
Method 1: Create an object with new
Using the new keyword is the most straightforward way to instantiate a class.
@Data
@NoArgsConstructor
@AllArgsConstructor
class GirlFriend {
private String name;
} /**
* new an object
*/
@Test
public void girlFriend1() {
GirlFriend girlFriend = new GirlFriend("new一个对象");
System.out.println(girlFriend);
}GirlFriend(name=new一个对象)
Method 2: Clone an object
Implement Cloneable and override clone() to produce a shallow copy.
@Data
@NoArgsConstructor
@AllArgsConstructor
class GirlFriend implements Cloneable {
private String name;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
} @Test
public void girlFriend2() throws CloneNotSupportedException {
GirlFriend girlFriend1 = new GirlFriend("克隆一个对象");
GirlFriend girlFriend2 = (GirlFriend) girlFriend1.clone();
System.out.println(girlFriend2);
}GirlFriend(name=克隆一个对象)
Method 3: Allocate an object via reflection (Class.newInstance())
@Test
public void girlFriend3() throws InstantiationException, IllegalAccessException {
GirlFriend girlFriend = GirlFriend.class.newInstance();
girlFriend.setName("类派发一个对象");
System.out.println(girlFriend);
}GirlFriend(name=类派发一个对象)
Method 4: Load and instantiate an object dynamically (Class.forName())
@Test
public void girlFriend4() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
GirlFriend girlFriend = (GirlFriend) Class.forName("cn.javastack.test.jdk.core.GirlFriend").newInstance();
girlFriend.setName("反射一个对象");
System.out.println(girlFriend);
}GirlFriend(name=反射一个对象)
Method 5: Construct an object via reflection (Constructor.newInstance())
@Test
public void girlFriend5() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
GirlFriend girlFriend = GirlFriend.class.getConstructor().newInstance();
girlFriend.setName("构造一个对象");
System.out.println(girlFriend);
}GirlFriend(name=构造一个对象)
Method 6: Deserialize an object
Make the class implement Serializable and use standard Java serialization APIs.
@Data
@NoArgsConstructor
@AllArgsConstructor
class GirlFriend implements Cloneable, Serializable {
private static final long serialVersionUID = 1L;
private String name;
@Override
protected Object clone() throws CloneNotSupportedException { return super.clone(); }
} @Test
public void girlFriend6() throws IOException, ClassNotFoundException {
GirlFriend girlFriend1 = new GirlFriend("反序列化一个对象");
// Serialize
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("gf.obj"));
oos.writeObject(girlFriend1);
oos.close();
// Deserialize
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("gf.obj"));
GirlFriend girlFriend2 = (GirlFriend) ois.readObject();
ois.close();
System.out.println(girlFriend2);
}GirlFriend(name=反序列化一个对象)
All example code is available on the author’s GitHub repository, and readers are encouraged to try these techniques or share other object‑creation methods.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
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.