nhaarman
nhaarman

Reputation: 100468

How to initialize Class<? extends MyClass>

I have an abstract method

public abstract Class<? extends MyClass> getMySpecialClass();

In another method I'd like to have an instant of this class. How would I do this?

public void method(){
   Class<? extends MyClass> mySpecialClass = getMySpecialClass();
   MyClass myClass = new ???
}

Upvotes: 1

Views: 1092

Answers (1)

Daniel Earwicker
Daniel Earwicker

Reputation: 116744

How about calling newInstance?

MyClass myClass = mySpecialClass.newInstance();

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#newInstance()

The class must have a nullary constructer (one with no parameters).

Upvotes: 4

Related Questions