difference between class level instantiation vs me

2019-01-28 19:55发布

what is difference between following variable usages

public class A{

    B b= new B();

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

VS

public class A { 
    B b;
    public void doSomething() {
        b=new B(); 
        b.callme();
    }
}

if we have single "b" inside a class then which one is better practice and why. and under what circumstances one whould be used.

6条回答
姐就是有狂的资本
2楼-- · 2019-01-28 20:24

Case 2: Is helpful for lazy initialization

In case 1 the object of B is created when you create object of A. But if creating B is heavy operation then you can lazily create it when you actually need B instance.

Lazy Initialization is helpful when the creating the object is a heavy task and you want to lazily create the object instance only when it is actually being used. But do take care of thread safety if your class is being shared between threads.

UPDATE: But in your case you are reassigning the reference b everytime the method is called. Which is not Lazy initialization per se.

//example of lazy initialization
public B getB()
{
  if (something =  = null)
    b = new B();
  return b;
}
查看更多
虎瘦雄心在
3楼-- · 2019-01-28 20:25

The first case is called inline initialization. It will happen before the body of any constructors run but after the call to the super constructor.

In the second case b is not initialized until doSomething is called().

As for which is better, that depends on your program logic. If you want a new instance every time doSomething is called, the second way is better. If you'd prefer to lazy load b then modify it to be

if (b == null) b = new B(); 
return b;

Personally I generally allocate instance variables in the constructor for the sake of readability.

public class A {
  B b;

  public A() {
    b = new B();
  } 
}
查看更多
劳资没心,怎么记你
4楼-- · 2019-01-28 20:28

These actually have very different meanings. In case 1, the b object is assigned when A is constructed. It is constructed once and only once (unless you are reassigning it from somewhere outside the class).

In case 2, you are reassigning A's instance of b every time the method is invoked

查看更多
Anthone
5楼-- · 2019-01-28 20:39

Class level instantiation will instantiate your class variables when new object of your class is created. While method instantiation will instantiate your variables when the method is invoked.

Good Practice : You should do Class level instantiation or instantiate in Constructor when your class variable is/must be final or else use method instantiation i.e lazy initialization

查看更多
爷的心禁止访问
6楼-- · 2019-01-28 20:45

The REAL difference here is that do you want a different instance of B each time doSomething is called? In the second case this is true but it also means that your class is not thread-safe if there are any other methods using B. And if there are NOT any other methods in the class using B why not make it a method-scoped variable?

查看更多
干净又极端
7楼-- · 2019-01-28 20:50

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.

查看更多
登录 后发表回答