1

I have the following code:

public class I<T> {
    private T t;
    public I(T t) {
        this.t=t;
    }
}

public class G<T> {
    private I<T> tab[];
    public G() {
        tab=(I<T>[]) new Object[10];
    }
}

Calling G() throws a ClassCastException.

How could I code the G constructor in order to initialize tab?

0

3 Answers 3

1
tab=(I<T>[]) new I<?>[10];

is the answer, but it is still mysterious for me!

Sign up to request clarification or add additional context in comments.

Comments

0

To hopefully demystify your own answer a bit: be aware, that java implements generics via erasure, i.e. the compiler basically does some compile time checks, and then discards the generics.

So, from the runtime point of view, your first approach boils down to:

I[] tab = (I[]) new Object[10];

As array types are really distinguished classes, you get a class cast exception here, as I[].class is not the same as Object[].class.

Your second approach is (generics discarded):

I[] tab = (I[]) new I[10];

... no problems here.

1 Comment

0

Tab is an array of I. I is an Object, but a simple Object created by new Object() is not a I. That explains why the first way produced an error. You must create a array of I to initialize tab.

But that's not all: java generics use type erasure. That means that at run time the compiler does not know what T is. tab = new I<T>[10]; is executed at run time when T has no meaning any more, hence the error.

The solution is to create an array of I of any (new I<?>) or an array of I<Object> or even use the old (pre-generic) syntax new I[10].

That's not asked here, but when you really need to know the type of the parameter in a generic, the common idiom is to pass explicitely the class in constructor or with a setter so that the generic object can use it. Code could be:

class G<T> {
    private I<T> tab[];
    private Class<T> clazz;
    ...
    public G(Class<T> clazz) {
        this.clazz = clazz;
        tab = new I[10];
        // here we can use the type of T from clazz by reflexion...
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.