I have found some blog where there is a suggestion of avoiding new
keyword while
creating object of a class. Some examples of creating object without the new
keyword are-
SampleObject obj = Class.forName("com.example.SampleObject").newInstance();
Or using clone()
method -
SampleObject obj1 = new SampleObject();
SampleObject obj2 = obj.clone();
Then here I have found some good examples of creating object without new
keyword
I can understand the advantages of 'Factory' pattern while avoiding new
keyword from main portion of code. If I am not using any design pattern (for creating objects) is there any benefit of creating object without the new
keyword? Or what are the reasons for creating of object without new
?