Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java generics type parameter hiding

I'm defining a class:

class Foo<I extends Bar & Comparable<I>> {
}

the compiler is complaining about I being hidden by I. I guess the second time I appears in the definition is hiding in scope the first one, as if variable I could be assigned to two different types. How to do it correctly?

Edit:

this is an inner class. the full code can be:

class Baz<I> {
    class Foo<I extends Bar & Comparable<I>> {
    }
}

now the problem is that if I re-nominate inner I to J, i'm not sure that I and J are actually the same types.

like image 963
marcorossi Avatar asked Dec 14 '11 22:12

marcorossi


1 Answers

Don't make the inner class parameterized:

class Baz<I extends Bar & Comparable<I>> {
   class Foo {
   }
}

As an inner (non-static nested) class, I as defined in the Baz declaration will still have meaning in Foo, since every Foo will have an implicit reference to its outer Baz instance.

like image 117
Paul Bellora Avatar answered Oct 26 '22 06:10

Paul Bellora