fork download
  1. class StopSelfRecursion {
  2.  
  3. // base class
  4. static class Class1<Self extends Class1<Self,T>, T> {
  5. public Self getMyself() {
  6. return (Self) this;
  7. }
  8. }
  9.  
  10. // derived 1
  11. static class Class2<Self extends Class2<Self, T>, T> extends Class1<Self, T> {
  12. }
  13.  
  14. // derived 2
  15. static class Class3<Self extends Class2<Self, T>, T> extends Class2<Self, T> {
  16. }
  17.  
  18. // want to stop recursion; want Class4 has only one parameter
  19. static class Class4<T> extends Class3<Class4<T>, T> {
  20. }
  21.  
  22. public static void main(String[] args) {
  23.  
  24. Class1<?, Integer> v1 = new Class1<>();
  25.  
  26. Class2<?, Integer> v2 = new Class2<>();
  27.  
  28. Class3<?, Integer> v3 = new Class3<>();
  29.  
  30. System.out.println(v1.toString());
  31. System.out.println(v2.toString());
  32. System.out.println(v3.toString());
  33.  
  34.  
  35. }
  36. }
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
StopSelfRecursion$Class1@10dea4e
StopSelfRecursion$Class2@647e05
StopSelfRecursion$Class3@1909752