Ben
Ben

Reputation: 399

Confusing compile error with Java generics and raw, unparameterized types

I am experiencing a somewhat counterintuitive compiler error when using generics. What I don't understand is why using the raw type here is causing such a failure. Has anyone else experienced this?

public class Test {

  public static void main() {

    // works
    Alpha<Void> a1 = null;
    a1.alpha().endBeta().endAlpha();

    // compile error: cannot find symbol 'endAlpha()'
    Alpha a2 = null;
    a2.alpha().endBeta().endAlpha();
  }

  interface Alpha<T> {
    Beta<Alpha<T>> alpha();

    T endAlpha();
  }

  interface Beta<T> {
    T endBeta();
  }
}

Upvotes: 0

Views: 71

Answers (1)

When you have a reference with a raw type, all generics are ignored when you call a method or use a field on that object.

This is one of many reasons you should not be using raw types in new code.

Upvotes: 2

Related Questions