Calling methods on reference variable vs Calling methods on a new object
Asked Answered
U

5

6

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?

Uriah answered 17/1, 2017 at 11:49 Comment(9)
In this particular case no, they have the exact same effect.Lapierre
definitely yes? because new A() makes a new A (because new A() and a are not the same object)Draff
There's a difference if you need to do something else with the new object afterwards, of course. I'm not sure what you're asking by "What will be the reference of new object in method-2." though - it's a reference to the newly created object...Chaco
@1blustone: Yes, but so does A a1 = new A();. Unless code uses a1 afterwards, they're equivalent - assigning the value to a local variable which isn't otherwise used won't make any difference.Chaco
In this case, wouldn't it just be better to make the method static as it needs no object to operate on?Draff
@Jon Skeet, now I have two object. First object's data can be accessed using a1 reference, but how can we access the second object data?Uriah
@Arun Sudhakaran you can't. that's the whole point.Farthermost
so can I say that method-2 is an improper way of calling a non-static method?Uriah
See here the difference:#29741012Hamner
C
2

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?

Collado answered 17/1, 2017 at 11:57 Comment(5)
so in method-2 new A() is an instance as well as a variable too?Uriah
@ArunSudhakaran, new A() is an instance, but not a variable (which just refers to a place where the instance lays)Collado
But an object without a reference variable is an abandoned object. So is the compiler adding some extra code in .class file for method-2, because in the class new A() is not having any reference variable?Uriah
No, it isn't. To execute a method, you need an instance. new A() returns the newly created object immediately (and puts it in the heap) while a variable gets an instance from the heap (it has been created before) by referenceCollado
@ArunSudhakaran, yes, there are no references to this objectCollado
C
4

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.

Constringe answered 17/1, 2017 at 12:3 Comment(0)
V
2

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.

Voroshilovgrad answered 17/1, 2017 at 11:55 Comment(0)
C
2

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?

Collado answered 17/1, 2017 at 11:57 Comment(5)
so in method-2 new A() is an instance as well as a variable too?Uriah
@ArunSudhakaran, new A() is an instance, but not a variable (which just refers to a place where the instance lays)Collado
But an object without a reference variable is an abandoned object. So is the compiler adding some extra code in .class file for method-2, because in the class new A() is not having any reference variable?Uriah
No, it isn't. To execute a method, you need an instance. new A() returns the newly created object immediately (and puts it in the heap) while a variable gets an instance from the heap (it has been created before) by referenceCollado
@ArunSudhakaran, yes, there are no references to this objectCollado
Y
2

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.

Yasmeen answered 17/1, 2017 at 11:59 Comment(0)
P
0

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.

Pyosis answered 6/8, 2019 at 6:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.