3

I am bringing myself up to date with Java generics (having worked for a loooong time on legacy code with JDK 1.4... 1.3 even) and I don't quite understand this:

public class Foo<T extends Bar<? extends Foo<T>>> { ...

Where Foo and Bar are two generic classes.

How is this to be understood because I don't quite get it?

I've read a lot about Java generics but this is a little mind bending (at least for me as a beginner).

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
ElenaT
  • 2,600
  • 4
  • 24
  • 41
  • possible duplicate of [Creating circular generic references](http://stackoverflow.com/questions/2567595/creating-circular-generic-references) – Brian Roach May 06 '11 at 17:00

1 Answers1

4

Well, Foo has to be parameterized by a T. That T itself has to extend Bar<U> for some type U such that U extends Foo<T>. (Where "extend" can mean "is the same type as" in this case.) I've used U here as an arbitrary type name, but it's unnamed in the declaration, hence ?.

You're right that it's a bit mind-bending, but typically in the circumstances where this sort of thing crops up, it ends up making things simpler. If you can give a concrete example, we may be able to explain a bit more usefully.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194