Java generics type parameters and operations on those types?

The reason why T. Class can't be accessed is because T is erased at compile time, so it doesn't exist at runtime to get a class.

The reason why T. Class can't be accessed is because T is erased at compile time, so it doesn't exist at runtime to get a class. The typical hack around this is to make a factory method: public static BaseDAOImpl createBaseDAO(Class klass) { return new BaseDAOImpl(klass); } And then in the constructor store the klass variable as a field and reference it when you need it.

You could use your interface (personally I would just go with a protected abstract method) if you want to keep a no-arg constructor. Protected abstract Class getGenericClass(); EDIT: In the case of the generic abstract superclass, there are a couple of options. One is have the constructor and then have the subclasses just have to call it (by not having a no-arg constructor).

Something like this: protected BaseDAOImpl(Class klass) { //store the parameter in a field. } Then the static method isn't relevant, as you have to create the subclass. The static factory method is more used when based on the class you can return the right implementation (so you have a factory, not just a strategy).

For the sake of completeness, I should point out that if the subclasses declare the generic when they extend the abstract class like this: public IntegerImpl extends BaseDAOImpl {} then the generic type is preserved in the class. There is a really ugly hack to get at this using the class. I experimented with this, and it worked: (Class) ((ParameterizedType) this.getClass().

GetGenericSuperclass()). GetActualTypeArguments()0 But it makes huge assumptions about the inheritance hierarchy, and should not be used in anything serious if it can at all be avoided, but I'm including it for the sake of a complete picture of what is going on.

As for your first suggestion, in my example BaseDAOImpl is an abstract class, I can't therefore write a statement such as "new BaseDAOImpl(klass)". In that respect what should I do and where should I place the factory method? – Yaneeve Oct 20 '09 at 16:48.

For some (probably good reason) T. Class causes a compile time error. My first question is why?

At compile time, compiler does not know the type T (in this case, it can be Integer, String), T. Class can't be supported to return the actual class. However, at the runtime, the type information is removed due to type erasure Cannot make a static reference to the non-static method getClass() from the type Object My second question is why?

And what does this error imply actually? Hm.. getClass() is class (non-static) member method, which require an object. T is not an object, it is a type, so it fails.

They don't make it static because there is already a XXX. Class keyword from which you can get the Class object.

– Yaneeve Oct 20 '09 at 16:38 1 Well, it depends on how smart the compiler treats this error. I am guessing compiler is first looking the object from which the getClass() is called.In this case, the object is absent, so it assumes it is trying to make static method calls, which does not require an object. At a result, it says your code is making to make a "static" reference calls which the method is non-static.

I know it is not clear, but it is much better than C++ template (i.e. Generic) compile error messages. – Oscar Chan Oct 20 '09 at 16:50.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions