When is it OK to create object of a class inside a method of that class?

前端 未结 6 1707
野的像风
野的像风 2021-02-01 05:15
public class TestClass(){
    public static void main(String []args) {
        TestClass t1 = new TestClass();
        t1.anything();
    }
}

Is it not

6条回答
  •  隐瞒了意图╮
    2021-02-01 05:50

    You would only have an infinite loop (stack overflow error) if you tried to do the below:

    public class TestClass {
        public TestClass() {
            TestClass t = new TestClass();
        }
    }
    

    And elsewhere, you try to create an object of the class TestClass.

提交回复
热议问题