This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object Creation

 
siva prakash
Ranch Hand
Posts: 80
  • 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
  • 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
  • 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