aspose file tools*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Bean.instanciate() Vs. new Object()" Watch "Bean.instanciate() Vs. new Object()" New topic
Author

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 MainServlet.java).
Thanks,
Francois
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">http://java.sun.com/j2se/1.3/docs/api/java/beans/Beans.html#instantiate(java.lang.ClassLoader, java.lang.String, java.beans.beancontext.BeanContext, java.beans.AppletInitializer)
David
------------------
David Kane
david_kane@houseofyin.com
Author of Software Architecture: Organizational Principles and Patterns
http://www.vraps.com
http://www.houseofyin.com


David Kane<BR> <A HREF="mailto:david_kane@houseofyin.com" rel="nofollow">david_kane@houseofyin.com</A> <BR>Author of <A HREF="http://www.amazon.com/exec/obidos/ASIN/0130290327/ref=ase_electricporkchop/103-0514572-3811868" TARGET=_blank rel="nofollow">Software Architecture: Organizational Principles and Patterns</A><BR><A HREF="http://www.vraps.com" TARGET=_blank rel="nofollow">http://www.vraps.com</A><BR><A HREF="http://www.houseofyin.com" TARGET=_blank rel="nofollow">http://www.houseofyin.com</A>
Marilyn de Queiroz
Sheriff

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

JavaBeginnersFaq
"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.
Matt
 
wood burning stoves
 
subject: Bean.instanciate() Vs. new Object()