Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use static List<String> within Comparator<String>

Why I have to fully qualify the java.lang.String in the static fields List

public static class MyComparator<String> implements Comparator<String> {

        public static List sortOrder;
        public static List<Integer> sortOrder2;
        public static List<java.lang.String> sortOrder3; // works!
        public static List<String> sortOrder4; // <-Compiler error only in this line


        @Override
        public int compare(String s1, String s2) {

            // TODO

            return -1;
        }
    }

Error is "Cannot make a static reference to the non-static type String"

like image 300
AlexWien Avatar asked Dec 06 '22 01:12

AlexWien


1 Answers

You have defined a generic type parameter String that is the same name as the class, so String refers to your parameter, while java.lang.String still works.

Remove it from your class, but keep it in the implements clause.

public static class MyComparator implements Comparator<String> {
like image 194
rgettman Avatar answered Feb 15 '23 03:02

rgettman