This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Making an object from a String variables value

 
Roshan Khandelwal
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say I have a string.. which obtains the name of a class after parsing an XML file....
Now i need to make an object of that particular class.. to set data in it.

However the class name is stored as the string variable value .
Is it possible to use it to make an instance of that class...

Example :



Now i want to make an object of type Test1....but using the value of string only.. Is it possible
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the Java Reflection classes.... java.lang.Class and the classes in the java.lang.reflect.* package.

Henry
 
Roshan Khandelwal
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Henry : Can you please give an example??
or maybe direct me to appropriate websires.. though i am trying it myself..
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roshan Khandelwal wrote:@Henry : Can you please give an example??
or maybe direct me to appropriate websires.. though i am trying it myself..



Just google for "java reflection". There are a few tutorials about it.

Or you can take a look at the two java classes in the JavaDoc that you will need -- java.lang.Class and java.lang.reflect.Constructor. Although, I recommend learning about the whole library than just two classes.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic