0

Why can't I declare a field with generics that implements multiple interfaces/classes?

class MyClass {
    private Class<? extends Enum<?> & Foo> bar;
    //..
}

interface Foo {
  // ...
}

public enum A implements Foo {
   // ...
}

Compiler error: Syntax error on token &

Nutel
  • 2,244
  • 2
  • 27
  • 50

1 Answers1

0

Question answered here.

In short, and quoted from the java language specification:

4.9 Intersection Types An intersection type takes the form T1 & ... & Tn, n>0, where Ti, 1in, are type expressions. Intersection types arise in the processes of capture conversion (§5.1.10) and type inference (§15.12.2.7). It is not possible to write an intersection type directly as part of a program; no syntax supports this. The values of an intersection type are those objects that are values of all of the types Ti, for 1in.

Community
  • 1
  • 1
PeterK
  • 1,697
  • 10
  • 20