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

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

    When you use new to create object constructors are called which initializes the instance variable this happens till all the constructors of your super class have been called . if you put the some code inside constructor that will run each time you create an object

提交回复
热议问题