Call methods by reference variable vs Methods of calling a new object

I got confused when 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? What will be the link to the new object in method-2 .

+6
source share
4 answers

Is there any functional difference?

Both will behave the same.

The second option does not allow you to reuse this instance again. This can be convenient and concise in single-line statements (for example, consider a builder pattern, where each construction method returns a semi-initialized instance):

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

or if the object was created only to perform a specific action once.

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

It no longer exists (more precisely, we do not have access to it) if doThis does not return this , which you could add to the variable after the method is executed.

Is method-2 the wrong way to call a non-static method?

Not. Why should we create a variable if this variable will never be used later?

+2
source

There will be no difference in the execution of these methods, but in the case of new A().doThis() you will lose a reference to the instance of the object to which you called the method, and you will not be able to use it further in your code. All changes that this method could make to the internal state of the instance will be lost.

In the case A a1 = new A(); a1.doThis(); A a1 = new A(); a1.doThis(); you will save an instance of the object (in the variable a1 ) and possible changes made by its state made by the doThis() method. Then you can continue to work with this object.

+4
source

See what the code says in plain language:

  A a1 = new A(); a1.doThis(); 
  • Create a new instance of A.
  • Save the link to it in the variable a1 .
  • Call doThis() on our instance.

While new A().doThis(); reads like:

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

So the only difference is whether you store it in a local variable or not. If you no longer use the value in a variable, then this difference does not matter. But if you want to call another method on the same object , say a1.doThat() , then you will have problems with the second solution, since you do not have a link to the original instance anymore.

Why do you want to use the same object? Since methods can change the internal state of an object, this is pretty much what the object is.

+2
source

Let's take a look at both of these methods one after another.

Method 1

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

In method-1 , you have a link to the newly created instance of A , that is, a1 , and you can call as many methods in this instance of A using this link a1 . Basically you can reuse this particular instance of A using its link a1 .

Method 2

 new A().doThis(); 

However, in method-2 , you do not have a variable in which the reference to the newly created instance of A is stored. How will you refer to this particular instance of A if you need to call any other method in that particular instance of A ? You cannot reuse this instance of A if you create an instance using method-2 and you lose this instance as soon as it is used.

+2
source

Source: https://habr.com/ru/post/1014148/


All Articles