public class A
{
private void sub()
{
add();
}
private void add()
{
-----
}
}
I can call the add method in sub like above and I can do the same as below
public class A
{
private void sub()
{
A obj_A = new A();
obj_A.add();
}
private void add()
{
-----
}
}
I would like to know the differences between them.
Thank you.
In the first method you are calling the
addmethod of the same instance of the class. In the second example you are creating a new instance of the class and calling itsaddmethod.For example:
In the first example, it will print 10. In the second one, it will print 3. This os because in the first example you are printing num of the instance itself that you have previously modified. In the second example you also modify num value but since you are invoking add of the new class you have created it will print 3.