0

C# allows for getting the type of generic parameters using the typeof(T)method without the need to instantiating the T parameter. But in Java anytime I have generic T parameter and I want to determine the type I have to create an instance of that parameter,for example by using the Class<T> Type, to figure out what type that is.

In comparison with what C# provides, this approach in Java looks unnecessarily lengthy and complicated. I would like to know what is best alternative to determine the type of a generic parameter without the need to instantiate that (for example if (T is Integer)).

JAX
  • 1,540
  • 3
  • 15
  • 32
  • There is no alternative. Provide the appropriate `Class` object. – Sotirios Delimanolis Jul 06 '14 at 21:32
  • possible duplicate of [How to determine the class of a generic type?](http://stackoverflow.com/questions/182636/how-to-determine-the-class-of-a-generic-type) – ᄂ ᄀ Jul 06 '14 at 21:32
  • C# has reified generics. Java doesn't and uses generic type erasure. There is no alternative to passing Class. – JB Nizet Jul 06 '14 at 21:33
  • @fnt: This question can never be a duplicate of that, I'm asking for a different way! – JAX Jul 06 '14 at 21:33
  • @PierreOverFlow Do you mind expanding on why you don't think this question is a duplicate? – awksp Jul 06 '14 at 21:44
  • 3
    @PierreOverFlow: It *really* looks like a duplicate of that - basically asking the same thing, but already knowing the answer. Yes, it's cumbersome, but that's *the* way in Java. – Jon Skeet Jul 06 '14 at 21:45
  • @user3580294: Because I'm already using the approaches explained in that post, I was looking for way to this WITHOUT instantiating the parameter, can you find an answer in that port that has explained something like that? – JAX Jul 06 '14 at 21:45
  • What do you mean by "instantiating the parameter"? – awksp Jul 06 '14 at 21:48
  • @user3580294: I concluded that it is not possible in java, for example the `typeof(T)` can tell what type is `T` in C#, but in java, you have to use `Class type` and instantiate that. (the approach in the other post) – JAX Jul 06 '14 at 21:50
  • 1
    @PierreOverFlow Yeah, that's pretty much it. I think you could have worded this question a bit differently ("How to retrieve the exact type of a generic parameter *without a type token*" or something like that; sounds like it might be different enough from the other question that it might not be dupe'd), but I think you'd probably have come up with the same answers. – awksp Jul 06 '14 at 21:51

2 Answers2

4

Generics in Java is a compile-time feature - thus the mismatch between Java and C#. As a result you cannot do anything at run time to determine the type unless you are either passed an object of the type or create one yourself or actually told the class in some other way.

It is generally considered a bad idea to even try to find the type. It generally indicates that you have not designed your class hierarchy properly.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • Though I agree to it being a bad idea in most programming situtations however for api developers and library developers it becomes a requirement in some caching scenarios. – AnthonyJClink Jun 14 '17 at 17:54
0

Generics are compile-time true... but you can give the compiler hints of what t really is.

by passing in the actual runtime class of what T really is, you allow the compiler to allow you runtime knowlege of the class T represents.

example:

public <T> boolean isObjectT(Class<T> type, Object object){
    return object.getClass().isAssignableFrom(type);
}

The answer on this question kinda spells out the limits of parameterized types:

Java: How do I specify a class of a class method argument?

If you are simply trying to get information from subclasses... you could try the reflection with paramterized types on this question:

How to determine the class of a generic type?

I have had good luck with that for more complex requirements.

Community
  • 1
  • 1
AnthonyJClink
  • 1,008
  • 2
  • 11
  • 32