Class Containing Reference of Itself
Why does Java allow having a reference of the class itself as its own class member, when it is prone to failure? A simple code like this one throws a StackOverflowError. public class Program { int value = 1; // regardless of whether or not this is static Program a; Program() { a = new Program(); } public static void main(String[] args) { Program b = new Program(); System.out.print(b.a.value); } } It seems contradicting to the nature of the language itself when Java compilers seemingly check most things and wouldn't even compile while (false). Of course, we can remove the initialization in the class constructor, but what is the point of allowing a reference of a class to be its own member in the first place?