File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object Creation

 
siva prakash
Ranch Hand
Posts: 80
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

i am going thru specification of java, and i found we can also create an instance of class using Class.forName(class_name).newInstance()

what advantage we can get using this approach instead of normal approach.

pls clarify me


With regards,
siva
 
M Beck
Ranch Hand
Posts: 323
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
with this way, you don't have to know the class name at compile time. see also the reflection tutorial for more information.
 
William A White
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't know what kind of object you want to create, this code lets you decide at run time.

You'd usually make use of it in conjunction with reflection, or some kind of factory that made different objects.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic