• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Making an object from a String variables value

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic