Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Making an object from a String variables value

 
Roshan Khandelwal
Ranch Hand
Posts: 35
  • 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: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 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
  • 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: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic