Object of the class as instance variable inside the class [duplicate]
Possible Duplicate: How can a class have a member of its own type, isnt this infinite recursion? The Code: public class Test2{ private Test2 subject = new Test2(); //Create Test2 object in Test2 private int num; } The Questions: Why does Java permit the above code to be executed, but C++ doesn't? Does the code above create infinite number of objects? Since Test2 itself contains a Test2 object which again contains a Test2 object which itself has a Test2 object and so on. The key difference between the two languages regarding your problem is that Java is a language with reference semantics (with