可能重复:
is there any other way of creating an object without using “new” keyword in java
在java中创建对象的方法有多少?
答案 0 :(得分:1)
//Using reflection
Class classObj Class.forName("Foo");
Foo obj1 (Foo)classObj.newInstance();
//new operator
Foo obj2 new Foo();
//cloning
Foo obj3 (TestObjectCreation)obj1.clone();
//deserialization
ByteArrayOutputStream baos new ByteArrayOutputStream();
ObjectOutputStream oos new ObjectOutputStream(baos);
oos.writeObject(obj1);
ByteArrayInputStream bais new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois new ObjectInputStream(bais);
Foo obj4 (Foo)ois.readObject();
obj4.method1();