difference between class level instantiation vs method instantiation

后端 未结 6 1563
慢半拍i
慢半拍i 2020-12-21 02:44

what is difference between following variable usages

public class A{

    B b= new B();

    public void doSomething()
    {
        b.callme();
    }
}
         


        
6条回答
  •  -上瘾入骨i
    2020-12-21 03:07

    I was just trying out a similar scenario, but due to a mistake I created a scenario for recursion instead (StackOverflow error):-

    public class Test{
    
    Test t=new Test();
    public static void main(String[] args) {
           Test t=new Test();
           System.out.println("Hello");
    }
    } 
    

    I think it might be helpful to some for the conceptual purpose.

提交回复
热议问题