Java generic field declaration

后端 未结 5 811
天命终不由人
天命终不由人 2021-01-11 12:01

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

public class Client {
    private Map

        
5条回答
  •  天命终不由人
    2021-01-11 12:59

    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 {
        private Map, List>> classToConsumerTry1;
    
        private Map, List>> 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.

提交回复
热议问题