Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Instantiate a class from the value of a string

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Marshal
Posts: 74069
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
  •  
    author and iconoclast
    Posts: 24203
    44
    Mac OS X Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.
     
    Campbell Ritchie
    Marshal
    Posts: 74069
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    That's useful information, Ernest, thank you.
    Now, can I remember it until I next need it???
     
    Ranch Hand
    Posts: 147
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks a lot guys. It's, really, very useful. I have 6 months JAVA programming experience, but there are always things that we miss.
     
    Vadim Vararu
    Ranch Hand
    Posts: 147
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    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?
     
    Hug your destiny! And hug this tiny ad:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic