Arun Sudhakaran
Arun Sudhakaran

Reputation: 2405

Calling methods on reference variable vs Calling methods on a new object

I'm having confusion in calling a non-static method

class A {
    void doThis() {}

    public static void main(String... arg) {
        A a1 = new A();
        a1.doThis();        // method - 1
        new A().doThis();   // method - 2
    }
}

I know that both method-1 and method-2 will call doThis(), but is there any functional difference?

Upvotes: 6

Views: 3174

Answers (5)

Chenchu
Chenchu

Reputation: 1

case1:

 A a1 = new A();
 a1.doThis(); 

The above two line means object created and doThis(); executed but still object available in the heap memory.

case2:

new A().doThis();

A class object created and doThis(); executed after immediately GC(GarbageColletor) will activate to remove the A object from the heap memory bcz it's a non-referenced object and we can call this object as an anonymous object.

Upvotes: 0

Yousaf
Yousaf

Reputation: 29282

Lets take a look at both these methods one by one.

Method-1

A a1 = new A();
a1.doThis();

In method-1, you have a reference of newly created instance of A, i.e a1 and you can call as many methods on this instance of A using this reference a1. Basically you can reuse that particular instance of A by using its reference a1.

Method-2

new A().doThis();

However in method-2, you don't have any variable that stores the reference of your newly created instance of A. How will you refer to that particular instance of A if you have to call any other method on that particular instance of A ? You will not be able to re-use that instance of A if you create an instance using method-2 and you will lose that instance as soon as it is used.

Upvotes: 2

Andrew
Andrew

Reputation: 49606

Is there any functional difference?

Both will behave in the same way.

The second option doesn't allow you to reuse that instance again. It may be convenient and concise in one-line return statements (for instance, consider the builder pattern where each constructing method returns a half-initialised instance):

return new Builder().a().b().build();

or if an object was created only to perform a defined action once.

What will be the reference of a new object in method-2?

It is no longer exist (more precisely, we don't have access to it) unless the doThis returns this which you could be able to put in a variable after method execution.

Can I say that method-2 is an improper way of calling a non-static method?

No. Why should we create a variable if this variable will never be used afterwards?

Upvotes: 2

Piotr Podraza
Piotr Podraza

Reputation: 2029

There won't be any difference in execution of those methods but in case of new A().doThis() your're going to lose the reference to the instance of an object you've invoked the method on and you won't be able to use it further in your code. All the changes this method could've done to internal state of the instance will be lost.

In case of A a1 = new A(); a1.doThis(); you're going to preserve the instance of an object (in variable a1) and potential changes made to its state made by method doThis(). Then you'll be able to continue working with this object.

Upvotes: 4

biziclop
biziclop

Reputation: 49714

Let's see what the code says in plain English:

      A a1 = new A();
      a1.doThis();
  1. Create a new instance of A.
  2. Store a reference to it in the variable a1.
  3. Call doThis() on our instance.

Whereas new A().doThis(); reads as:

  1. Create a new instance of A.
  2. Call doThis() on our instance.

So the only difference is whether you store it in a local variable or not. If you don't use the value in the variable any more, then that difference doesn't matter. But if you want to call another method on the same object, let's say a1.doThat(), then you're in trouble with the second solution, as you haven't got a reference to the original instance any more.

Why would you want to use the same object? Because methods can change the internal state of the object, that's pretty much what being an object is about.

Upvotes: 2

Related Questions