This week's book giveaway is in the Agile forum.
We're giving away four copies of The Software Craftsman and have Sandro Mancuso on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Bean.instanciate() Vs. new Object() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Bean.instanciate() Vs. new Object()" Watch "Bean.instanciate() Vs. new Object()" New topic

Bean.instanciate() Vs. new Object()

Paul Medford
Ranch Hand

Joined: Aug 28, 2001
Posts: 33
Does anyone know the difference between creating an object using the new keyword and Beans.instantiate() as shown below?
modelManager = (ModelManager) Beans.instantiate(this.getClass().getClassLoader(), "com.sun.j2ee.blueprints.petstore.control.web.ModelManager");
(Code taken from the PetStore
David Kane
Ranch Hand

Joined: Sep 20, 2001
Posts: 65
new will always create a new object.
Bean instantiate may reuse an existing serialized instance if available. Check out: java.lang.ClassLoader," TARGET=_blank rel="nofollow">, java.lang.String, java.beans.beancontext.BeanContext, java.beans.AppletInitializer)
David Kane
Author of Software Architecture: Organizational Principles and Patterns

David Kane<BR> <A HREF="" rel="nofollow"></A> <BR>Author of <A HREF="" TARGET=_blank rel="nofollow">Software Architecture: Organizational Principles and Patterns</A><BR><A HREF="" TARGET=_blank rel="nofollow"></A><BR><A HREF="" TARGET=_blank rel="nofollow"></A>
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9056
I think there is no difference in the end result. Using the instantiate() method is a little more flexible.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Matt Siegel
Ranch Hand

Joined: Jul 18, 2000
Posts: 55
To me the biggest difference is when you use instantiate(), you don't have to know the Class name of the object at development time. The class that you want an instance of can be determined at runtime from a String.
I agree. Here's the link:
subject: Bean.instanciate() Vs. new Object()