4

I'm trying to create an array of classes in Java. Not objects, but classes. Currently I have a class MyBaseClass and I extend three classes MyClass1, MyClass2, and MyClass3 from it. I store these classes to a static array like this:

private static MyBaseClass[] classes = {
        new MyClass1(),
        new MyClass2(),
        new MyClass3()
};

public static MyBaseClass getInstanceOfClass(int index) {
    return classes[index];
}

and then I use those methods like this:

try {
    MyBaseClass obj = getInstanceOfClass(index).getClass().newInstance();
} catch (InstantiationException | IllegalAccessException e) {
    e.printStackTrace();
}

Now I'm wondering if I could do this in an easier way. If I could create an array of classes I might be able to escape the getInstanceOfClass() method and some possible exceptions. I tried doing this:

private static Class<MyBaseClass>[] classes = {
        MyClass1.class,
        MyClass2.class,
        MyClass3.class
};

But this gives me an error "Incompatible types" as MyClass1 is not equal to MyBaseClass. Interestingly enough, this seemingly works:

private static Class<?>[] classes = {
        new MyClass1().getClass(),
        new MyClass2().getClass(),
        new MyClass3().getClass()
};

But the idea of that is horrible and it's even marked by my debugger. So, any better ways of doing this?

Edit:

This works:

private static Class<?>[] classes = {
        MyClass1.class,
        MyClass2.class,
        MyClass3.class
};

But then the result of getInstanceOfClass(index).newInstance(); is an Object so I have to do typecasting. I'm not really sure if that's safe in this case...

2
  • I dont think you can create arrays of generic type,due to type erasure Commented Aug 11, 2014 at 16:50
  • The most unnerving thing in this is that at least Android Studio doesn't mark Class<?>[] or Class<MyBaseClass>[] with an error. But Class<? extends MyBaseClass>[] is marked. Commented Aug 12, 2014 at 17:45

1 Answer 1

5

You have to indicated that you are looking for possibly subclasses of MyBaseClass. For this you can add ? extends to the generic type description. Note that this will also accept MyBaseClass itself.

Added to this, you can use .class to get the Class object of a class, instead of making an isntance and calling getClass().

Unfortunately this doesn't work with arrays because they don't allow generic types. So you have to add them to a list (which is almost always a good thing):

private static ArrayList<Class<? extends MyBaseClass>> classes = new ArrayList<Class<? extends MyBaseClass>>();

...

classes.add(MyClass1.class),
classes.add(MyClass2.class),
classes.add(MyClass3.class),
Sign up to request clarification or add additional context in comments.

8 Comments

This gets marked as an error "Generic array creation".
Ah you are right, arrays don't allow subclasses, you can store them in a List.
I don't have a List. The code you posted above gets marked with an error. Funny enough that Class<?>[] or Class<MyBaseClass>[] doesn't get marked but Class<? extends MyBaseClass>[]gets.
From java 7 ,you can use diamond operator too
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.