Classes are first class objects in Java (Class class). You can assign them to variables, pass, return from a function, etc. I think you wanted to ask another question, but I'm not sure what this question may be. Maybe something about the erasure and reification of the generic types? The answer to this may be interesting.
An example of what you keep writing can help.
Also, consider the difference between the question, for example, “Does X have a reason” and “Does X have a good use”.
EDIT: (replying to the comment: “I have not seen any examples of classes that are passed” (by the way, I still think this is a question about erasing or materializing, but this specific comment don’t refer to it). I am surprised that you They didn’t. Class passing in Java is very common. A few examples from the really popular APIs that appear in my head without hesitation:
The Hibernate / JPA Entity Administrator scans the associated object based on its class and its primary key; e.g. Invoice i = entityManager.find(Invoice.class, 12l)
GWT uses a special factory to inject classes that exist only in generated javascript (or otherwise parameterized); The method takes an instance of the class to create; for example: Resource res1 = GWT.create(MyResources.class);
Spring ApplicationContext gives you beans based on the class that you pass to the getBean method; so what would you do: DataSource default = applicationContext.getBean(DataSource.class);
Instance instances are used in rare cases when C # will use reflection (and Java cannot, since it erases common types at runtime); the template is sometimes called a "class token"
In most cases above, you will see a class literal (since first-class class classes have literals in Java), not a dynamic call, but mainly because of the static nature of the language (and the programmers who use this). It is generally considered good to know your types at compile time.
since Java does not support generics at run time, you cannot create a generic class on the fly. This doesn't make any difference since you can create a non-generic class and use it as a generic one.
Responding to another comment: the creation and modification of classes is usually used at runtime, but mainly using the Java infrastructure: application servers, libraries. Look at J. Mokkita. In fact, you can modify an existing class or replace its methods on the fly by the time the method is called.
source share