DEV Community

Discussion on: Is there vararg-like generics in Java?

Collapse
baenencalin profile image
Calin Baenen Author

Oh. Is there any way I can include more than one, but a never finite amount of generics (safely)?

I really want to use it for something like this:

class Container<...T> {
    public Container(String[] names, Object[] values) {

        int pos = 0;
        for(String name : names) {
            Class Type = T[pos]; // T-POS!?!?
            /* Code to make a fake class ere */
            pos++;
        }

    }
}
Enter fullscreen mode Exit fullscreen mode

Having something like that'd be cool.

Thanks for you answer.
Cheers.

Collapse
zoedreams profile image
☮️✝️☪️🕉☸️✡️☯️

nope you can only have one, and it has to be the first one.

you can not use T like an object, it is just a reference to what type you can cast object to,,

T obj = values[pos] // or names[
Enter fullscreen mode Exit fullscreen mode

also you dont need the pos or pos++. i would recommend using a regular for loop with index variables.

what is it your actually trying to do. I am confused with the T[pos]

T is not an object it is the name of object called T.

what is it your actually trying to do?