Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of Classes in Java

Tags:

java

generics

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);
like image 212
Rob Fox Avatar asked Apr 10 '11 17:04

Rob Fox


People also ask

How many classes are there in Java?

There are 5,000 or so classes built-in to Java, and programmers have written hundreds of thousands if not millions of their own.

Can you make a list of classes in Java?

The classes LinkedList, Stack, Vector, ArrayList, and CopyOnWriteArrayList are all the implementation classes of List interface that are frequently used by programmers. Thus there are four types of lists in Java i.e. Stack, LinkedList, ArrayList, and Vector.

What is list of Lists in Java?

The list is an interface in Java, which is a child interface of Collection. You can access it using java. util package. The classes that implement the List interface in Java are LinkedList, ArrayList, Vector, Stack, etc.


2 Answers

List<Class<? extends A>> classes = new ArrayList<Class<? extends A>>(); classes.add(B.class); classes.add(C.class); classes.add(D.class); 
like image 104
CarlosZ Avatar answered Sep 21 '22 08:09

CarlosZ


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
};
like image 27
T.J. Crowder Avatar answered Sep 18 '22 08:09

T.J. Crowder