Search code examples
javagenericsglobal-variablescomparable

Java generics to assign global variables


In this example, I get the warning that "List is a raw type." How can I use generics to properly satisfy this warning? I'm having trouble because I can't figure out how to define "T" for the global variable like I was able to for the constructor signature.

public class MyClass{

    private List input;

    public <T extends Comparable<T>> MyClass(List<T> input){
        this.input = input;
    }

}

Solution

  • You must define your class as parameterized

    public class MyClass<T extends Comparable<T>>{
    
        private List<T> input;
    
        public MyClass(List<T> input){
            this.input = input;
        }
    
    }