So, I did some tests to figure this out, and here are my test cases to see how you can use this general case:
public class A<B extends A<B>> { int x = 10; B test; void printX() { System.out.println(x); } void setB(B b) { test = b; } void printB() { System.out.println(test); } } public class B extends A<B> { } public class Run { public static void main(String[] args) { A<B> test = new A<B>(); B myB = new B(); test.printX(); test.setB(myB); test.printB(); myB.printB(); } }
I hope the code can be understood. If you do not leave a comment, I will try to explain what is happening. Look at the last line of myB.printB (), here we get zero, because B is not set for myB yet, but only for the test. This demonstrates that we can have infinite recursion into classes B inside A (and inside B for that matter).
can say:
myB.test.printB();
This will lead to an error (null pointer), but it will show that we now have access to the test in class A from B, and we can go as deep as we want recursively, with as many cases as we like. So class A works like a wrapper for an infinite number of classes B. Does that make sense?
Arash saidi
source share