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

前端 未结 6 1848
野的像风
野的像风 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:40

    It's not really odd. All object oriented languages that I am aware of allow this. The code is semantically part of the object definition, but in practice it can be considered separate from the actual state of any given object. So there is no loop because object construction doesn't call your method (unless, of course, it does - then you have a problem).

提交回复
热议问题