aspose file tools
The moose likes Java in General and the fly likes Instantiate a class from the value of a string Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Instantiate a class from the value of a string" Watch "Instantiate a class from the value of a string" New topic
Author

Instantiate a class from the value of a string

Larry Homes
Greenhorn

Joined: Jan 18, 2009
Posts: 25
Hello,

I was wondering if there was a way to instantiate a class from the value of a string. Here's an example:

There is a main class that prompts the user for a string. Say the user enters "Hello". Assuming Hello is an instantiable class, is there a way for the main class to take that string and create a Hello object?

Also, assume that there are too many classes defined that the user might type in for a switch case statement to be feasable or reliable.

If anything is unclear, please ask. Thanks for the help!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
Welcome to JavaRanch

Go through the Class class, and you find you can load a Class object with the forName method. When you have got your hands on that Class object, you can call the newInstance() method on it.

Foo fff = Class.forName("Foo").newInstance();

Beware:
  • The tiniest spelling error and you will get Exceptions
  • the newInstance method probably declares a checked Exception.
  • You usually need to provide the fully-qualified name of the class.
  • I think you can only instantiate classes like that if they have a no-arguments constructor.
  • You might need to cast the resultant object, but I am not sure on this last point. It probably depends on whether you have got a formal type parameter anywhere.
  • Ernest Friedman-Hill
    author and iconoclast
    Marshal

    Joined: Jul 08, 2003
    Posts: 24041
        
      13

    Campbell Ritchie wrote:I think you can only instantiate classes like that if they have a no-arguments constructor.


    If you need to create an instance of a class that doesn't have a no-argument constructor, you can use the several "getConstructors()" methods of java.lang.Class to allow you to call any other constructor.


    [Jess in Action][AskingGoodQuestions]
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 32599
        
        4
    That's useful information, Ernest, thank you.
    Now, can I remember it until I next need it???
    Vadim Vararu
    Ranch Hand

    Joined: Jan 03, 2009
    Posts: 147
    Thanks a lot guys. It's, really, very useful. I have 6 months JAVA programming experience, but there are always things that we miss.


    If you think you've done too much, usually it means you've done too few.
    Vadim Vararu
    Ranch Hand

    Joined: Jan 03, 2009
    Posts: 147


    This throws an exception like this, when i have a constructor without modifiers at all. As i know we CAN have constructors with default modifier access. Then, what's the problem?


    Probably, it's because i try to access a default modifier from another package? Is this the reason? Is there a way to work with any modifiers from anywhere?
     
    I agree. Here's the link: http://zeroturnaround.com/jrebel/download
     
    subject: Instantiate a class from the value of a string
     
    Similar Threads
    JCheckBox itemListener
    Class Loading & Variables
    Multiple versions of a component - design question
    Ways to invoke a method in static inner class
    throw UserInterface GUI via Session Bean