简体   繁体   中英

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.

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.

If I is already defined in the outer class just make this

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

You cannot redefine I in your inner class. The I of the inner class would be something else than the I of the outer class, if this is what you want, well, rename it.

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM