|
|
||||||
|
#1
|
|
|
|
|
Why does the Class type use Generics?
Or in other words - how am I supposed to use it? I store certain Classes in hash tables, and then create dynamic instances of objects according to the correct type at run-time. What "type of class" should my Class variables be?? |
|
|
|
#2
|
|
|
|
|
Josef Garvi wrote:
> Why does the Class type use Generics? > > Or in other words - how am I supposed to use it? > I store certain Classes in hash tables, and then create dynamic > instances of objects according to the correct type at run-time. What > "type of class" should my Class variables be?? It depends, but some way around you're going to need to use a wildcard type parameter. Class<?> will definitely work, but you may be able to use something more specific. For instance, if you are storing only Class objects for classes that implement Serializable, you could use Class<? extends Serializable>. You may indeed want to do this sort of thing to achieve type safety and dispense with casting when you create instances: Class<? extends Serializable> clazz; [...] Serializable ser = clazz.newInstance(); /* typesafe; no cast required */ |
|
#3
|
|
|
|
|
John C. Bollinger wrote:
> It depends, but some way around you're going to need to use a wildcard > type parameter. Class<?> will definitely work, but you may be able to > use something more specific. For instance, if you are storing only > Class objects for classes that implement Serializable, you could use > Class<? extends Serializable>. You may indeed want to do this sort of > thing to achieve type safety and dispense with casting when you create > instances: > > Class<? extends Serializable> clazz; > > [...] > > Serializable ser = clazz.newInstance(); /* typesafe; no cast required */ Thanks. That's how I wanted it to work! :-) |
|
|
| Similar Threads | |
| Class Generics Class modelClass = Class.forName(modelResult.getNodeValue()); Class viewClass = Class.forName(viewResult.getNodeValue()); factory.put(modelClass, viewClass); I get the... |
|
| Generics passing Class type I'm currently creating a custom column in a DataGridView. I'm wondering if Generics can be used to solve an issue I'm seeing. When creating the editing control for the... |
|
| Calling a Static Method on a Class Type passed as a Generics Argument. Hi, I'm trying to call a static method on a class type that is passed into a method through generics, for example: public bool Test<TestStaticType>() where TestStaticType :... |
|
| question on generics in class type declarations I have a simple problem using generics. Suppose I want to make a parameterized holder class, call it Singleton, that contains a single object o, and instances of Singleton... |
|
| question on generics in class type declarations I have a simple problem using generics. Suppose I want to make a parameterized holder class, call it Singleton, that contains a single object o, and instances of Singleton... |
|
|
All times are GMT. The time now is 02:41 PM. | Privacy Policy
|