File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bean.instanciate() Vs. new Object()

 
Paul Medford
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there is no difference in the end result. Using the instantiate() method is a little more flexible.
 
Matt Siegel
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic