what is difference between following variable usages
public class A{
B b= new B();
public void doSomething()
{
b.callme();
}
}
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();
}
}