2

I am curious about what specific rules of Java's type erasure cause the following to happen:

import java.util.function.Supplier;

public class TypeCheckerWeirdness {

    public static class Meow<S extends Meow> {

        public Supplier<Boolean> hungry;

    }

    public static void main(String[] args) {
        Meow<?> instanceA = new Meow<>();
        Meow instanceB = new Meow<>();

        // fine
        boolean x1 = instanceA.hungry.get();

        // incompatible types. Found: 'java.lang.Object', required: 'boolean'
        boolean x2 = instanceB.hungry.get();
    }

}

Is it because of that if you use a raw type, all generics information on the members of the type will be erased even at the compilation time?

Haochen Xie
  • 360
  • 2
  • 9
  • 1
    Yep, your intuition is correct, though I don't know if *erasure* is the correct term for what's happening here. Take a look at this question: http://stackoverflow.com/q/11007723/228171 – Mark Peters Mar 12 '16 at 12:44

0 Answers0