Popular Posts

Friday, April 03, 2015

How many ways we can create object in Java

Possible ways to create new instance in Java

1) Using constructor - 
a - using new  
b - using reflection (again two ways - Constructor.newInstance() and Class.newInstance(), first to be preferred as it may invoke any constructor, regardless of the number of parameters - second one calls only default c-tor)
c - Class.forName("your.cClass").newInstance();

2) via pseudo-constructors - 
a - clone() 
b - readObject() [or deserialization]

No comments: