-3

Possible Duplicate:
is there any other way of creating an object without using β€œnew” keyword in java

how many ways are there to create an object in java ?

Community
  • 1
  • 1
Bipin Sahoo
  • 201
  • 1
  • 2
  • 6

1 Answers1

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();
Amir Afghani
  • 37,814
  • 16
  • 84
  • 124