1
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.

Wojciech Wirzbicki
  • 3,887
  • 6
  • 36
  • 59
Raghurocks
  • 917
  • 7
  • 17

6 Answers6

4
  • If you create an instance of A with the first class and invoke sub() you will have 1 instance of A within the method scope.
  • If you create an instance of A with the second class and invoke sub() you will have 2 instances of A wihin the method scope.
Juvanis
  • 25,802
  • 5
  • 69
  • 87
2

Java classes have a special member defined called this which refers to the current object.

This answer will give you more details on this.

Community
  • 1
  • 1
Xetius
  • 44,755
  • 24
  • 88
  • 123
1

In the first method you are calling the add method of the same instance of the class. In the second example you are creating a new instance of the class and calling its add method.

For example:

public class A
    {
        private int num = 3;

        private void sub()
        {
          num = 10;            
          add();
        }
        private void add()
        { 
              system.out.println(num);
        }
    }

public class A
{
    private int num = 3;

    private void sub()
    {
        A obj_A = new A();
        num = 10;      
        obj_A.add();
    }
    private void add()
    { 
           system.out.println(num);
    }
}

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.

Averroes
  • 4,168
  • 6
  • 50
  • 63
  • but in the second case if I assign num as 10 in the sub and then call add then num will be again 10. – Raghurocks Dec 21 '12 at 07:29
  • 1
    @Raghurocks.. No num will not be 10 there. A different instance is running the `add()` method. And in `sub()` method, you are setting `num = 10`, for different instance. – Rohit Jain Dec 21 '12 at 07:31
  • 1
    @Raghurocks.. Currently it is - `this.num = 10;` To change num for new object, you need - `obj_A.num = 10;`. – Rohit Jain Dec 21 '12 at 07:31
  • 1
    @Raghurocks And what if num doesn't have public accessors? This should show roughly what is happening. You have two values of `num`, one on each instance of `class A`. In your second example you create a whole new instance of A. – Averroes Dec 21 '12 at 07:32
  • @RohitJain you said 2 instances will be created in the second case, and 1 instance will be created in the 1st case, so when will an instance will be created? when we call a method? – Raghurocks Dec 21 '12 at 08:47
  • 1
    @Raghurocks.. When you first time call `sub()` method from `main`, you are creating an instance of `A`, right. Now, in `sub()` method, you are again creating an instance of `A()`, to call `add()` method. Whereas in 1st case, you are invoking the `add()` method on the same reference `this`. `this` is a reference to the current instance executing. So, in first case, `this` refers to the same instance in both `add()` and `sub()` method. Whereas in 2nd case, `this` refers to different instances in both methods. – Rohit Jain Dec 21 '12 at 08:51
  • Ok thanks for your help , so I can confirm that when we call a method from main we are creating an instance of that class right?.Thank you so much for help. – Raghurocks Dec 21 '12 at 08:59
0
private void sub()
{
  add();
}

Calling method add() on the same object which called sub() method.

private void sub()
{
   A obj_A = new A();
   obj_A.add();
}

Creating new object of type A and calling add() method on it.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ajinkya
  • 22,324
  • 33
  • 110
  • 161
0

By second method, to invoke add method two objects will be created, by using the method 1 we can access add method by just creating one object

Subin_Learner
  • 347
  • 1
  • 4
  • 15
0

In second approch unneciserly we are creating Object.

First approch is better.

NPKR
  • 5,368
  • 4
  • 31
  • 48