limited time offer

How many ways you can create an Object in Java?

There are the five ways to create the object or get the object in java:

1. By using the new operator

Test t = new Test();

2.  By using the newInstance() Method

Test t = (Test)class.forName("Test").newInstance();

3. By using the factory Method

Runtime r = Runtime.getRuntime();
DateFormat d = Dateformat.getInstance();

4. By using the clone() Method

Test t = new Test();
Test t1= (Test)t.clone();

5. Bu using the deserialization

FileInputStream fis = new FileInputStream("Vinay");
ObjectInputStream obj = new ObjectInputStream(fis);
Dog d = (Dog)obj.readObject();

Not:- There is another way to create the object in Java but this is not standard, it is applicable only for the String

String s = "Vinay";