Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java generic field declaration

In a class without generic types I want to declare a rather complex generic field similar to these:

public class Client {
    private Map<Class<T extends Serializable>, List<Consumer<S extends T>>> classToConsumerTry1;

    private <T extends Serializable, S extends T> Map<Class<T>, List<Consumer<S>>> classToConsumerTry2;
}

promblem is the java compiler won't let me :)

So my question is how do I correctly introduce T and S without adding types to my class Client.

My goal is to enforce the Class being a subtype of Serializable and the Consumer being a subtype of the class you chose for Class.

like image 313
succcubbus Avatar asked Aug 14 '15 11:08

succcubbus


2 Answers

You can't. Your only option is to declare the generic type parameters in your Client class declaration. If your Client class has no generic type parameters, its members can't be generic. You must use actual types in the declaration of your class members.

like image 156
Eran Avatar answered Oct 01 '22 11:10

Eran


You have to somewhere introduce the type-parameter, so that you can use them in the definition for your class members.

Introducing a type-parameter can be done only on a class-level, or on a method-level. In your case, it should be on class-level:

public class Client<T extends Serializable, S extends T> {
    private Map<Class<T>, List<Consumer<S>>> classToConsumerTry1;

    private Map<Class<T>, List<Consumer<S>>> classToConsumerTry2;
}

This, however, implies that for both members (classToConsumerTry1 and classToConsumerTry2), T and S are the same. If you want them to be different, the you will have to get these two values from two different classes, both of which are parameterized with separate type-parameters.

like image 21
Konstantin Yovkov Avatar answered Oct 01 '22 13:10

Konstantin Yovkov