Java generics: Use this type as return type?
问题 I'm trying to make an API as user friendly as possible. Let's have: class B extends A {} class A { A setX(){ ...; return this; } } Now this B b = new B().setX(); is invalid, has to be casted: B b = (B) new B().setX(); Is there a way using generics in A to make compiler aware of "this" type and accept the first way - without casting and without passing type parameter at the place where used? (I.e. not new B<B>().setX() , that's ugly.) I KNOW why Java needs retyping in this case. Please no