Search code examples
javagenericsnested-generics

Infinite nesting in java generics


Today I noticed that if you declare the following in Java:

public interface Foo<T extends Foo> {}

then you can recursively declare objects of the type:

Foo<Foo<Foo<Foo<Foo<Foo<Foo... ...>>>>>> foo;

and you will never hit the end of it, but you will never be able to satisfy the warning: "Foo is a raw type. References to the type Foo should be parameterized. Very curious.


Solution

  • There is a way to get rid of the warning :

    public interface Foo<T extends Foo<?>> {}
    

    and

    Foo<Foo<Foo<Foo<Foo<Foo<Foo<?>>>>>>> foo;