Rob Fox
Rob Fox

Reputation: 5581

List of Classes in Java

I have multiple classes (B, C and D) that are subclasses of class A. I need to make a List/Array containing B, C and D and create Objects based on whatever element I pull from the List/Array.

In AS3 I would do something like this: var classes:Array = [MovieClip, Sprite, Shape]; or a Vector of Classes.

How do I do this in Java? I'm thinking about something like this right now:

List<Class<? extends A>> list = new ArrayList<Class<? extends A>>();

list.add(B);

Upvotes: 31

Views: 57768

Answers (2)

T.J. Crowder
T.J. Crowder

Reputation: 1074198

You can do analogues of both of those. As CarlosZ pointed out, there's List and its various implementations, or you can create an array:

Class[] classes = new Class[] {
    MovieClip.class, Sprite.class, Shape.class
};

Upvotes: 4

CarlosZ
CarlosZ

Reputation: 8669

List<Class<? extends A>> classes = new ArrayList<Class<? extends A>>();
classes.add(B.class);
classes.add(C.class);
classes.add(D.class);

Upvotes: 39

Related Questions