Ways of Creating Object in Java - rahul00773/JavaConcepts GitHub Wiki

How many we can create an Object in java or In how many we can get the object in java?

Ans: There are total of 5 ways:

1. By Using New Operator

Test t = new Test();

2. By Using factory Method

Runtime r = Runtime.getRuntime();

DateFormat df = DateFormat.getInstance();

3. By Using New Instance Method

Test t= (Test).class.forName(“Test”).newInstance();

4. By using clone() method

Test t1 = new Test();

Test t2= (Test)t1.clone();

5. By Using deserialization

FileInputStream fis = new Fis(“abc”); ObjectInpputStream ois = new OIS(fis); Dog d2 =(Dog)ois.readObject();