Why does this java code produce StackOverflowError? I understand that this somehow connected with recursive generic type parameter. But I don’t understand clear the whole mechanism.
public class SomeClass<T extends SomeClass> {
SomeClass() {
new SomeClassKiller();
}
private class SomeClassKiller extends SomeClass<T> {
}
public static void main(String[] args) {
new SomeClass();
}
}
The generic part doesn’t matter – nor does it really matter that the class is nested. Look at this mostly-equivalent pair of classes and it should be more obvious:
So the subclass constructor calls the superclass constructor – which then creates a new subclass, which calls into the superclass constructor, which creates a new subclass, etc… bang!