DEV Community

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

zoedreams profile image



... is basically just shorthand for an array of like types.

by allowing would create an unsafe operation which could potentially allow for buffer overflows or other nasty things.

you can however recursively call the parent object using an anonymous type of <?> which then injects the next type in the array of reflected types into the root. I would advise against that due to circular dependency.

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 */

Enter fullscreen mode Exit fullscreen mode

Having something like that'd be cool.

Thanks for you answer.

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?