-2

I would like to make a method, that would make instance of my generic class. The type of this object have to be specified by given String. Prototype looks like this:

public class SomeClass {


    static BST<?> bst;

    public static void main(String[] args)
    {
        MakeInstance("String");
    }

    static <T extends Comparable<T>> void MakeInstance(String input)
    {
        try {
            bst = (BST<?>) Class.forName(input).newInstance();
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

I would like to type for example "Integer" and method should create object of BST of type Integer (BST)

J.Kennsy
  • 596
  • 1
  • 6
  • 19
  • 4
    Possible duplicate of [Dynamic Generic Typing in Java](http://stackoverflow.com/questions/7342035/dynamic-generic-typing-in-java) – Michael Markidis Jun 02 '16 at 16:58

1 Answers1

0

There is no such thing as reflectively creating a BST of type Integer. You will just create a raw BST and cast it:

bst = new BST();
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413