From the below example, In Case 1 the object is created in Class level and in Case 2 its created in Method level.
My understanding that in Case 2 once the method completes its execution the object is removed from heap memory . Is my understanding correct ?
Now My question is,in both the cases when will the object be removed from the Heap memory and which is efficient way of using in different context ?
public class A()
{
ClassB obj = new ClassB(); // Case 1
private void method()
{
ClassB obj = new ClassB(); // Case 2
}
}