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.
Case 2:
Is helpful for lazy initializationIn case 1
the object ofB
is created when you create object ofA
. But if creatingB
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 ofthread 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.
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
Personally I generally allocate instance variables in the constructor for the sake of readability.
These actually have very different meanings. In case 1, the
b
object is assigned whenA
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 ofb
every time the method is invokedClass level instantiation
will instantiate your class variables when new object of your class is created. Whilemethod instantiation
will instantiate your variables when the method is invoked.Good Practice : You should do
Class level instantiation
orinstantiate in Constructor
when your class variable is/must befinal
or else usemethod instantiation
i.elazy initialization
The REAL difference here is that do you want a different instance of
B
each timedoSomething
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 usingB
. And if there are NOT any other methods in the class usingB
why not make it a method-scoped variable?I was just trying out a similar scenario, but due to a mistake I created a scenario for recursion instead (StackOverflow error):-
I think it might be helpful to some for the conceptual purpose.