Popular Posts

Thursday, June 05, 2014

In how many different ways I can create new java object?

What are all the different ways to create new object / instance in Java?

All / Best Possible ways to create instance :

1) via 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]

1 comment:

vini vinitha said...

Nice article, Thanks for sharing and keep sharing.
upcoming college events | upcoming campus festivals