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?

Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52
  • 1
    In this particular case no, they have the exact same effect. – BackSlash Jan 17 '17 at 11:50
  • definitely yes? because `new A()` makes a new `A` (because `new A()` and `a` are **not** the same object) – Salem Jan 17 '17 at 11:50
  • 2
    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... – Jon Skeet Jan 17 '17 at 11:50
  • 2
    @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. – Jon Skeet Jan 17 '17 at 11:51
  • 1
    In this case, wouldn't it just be better to make the method static as it needs no object to operate on? – Salem Jan 17 '17 at 11:54
  • **@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? – Arun Sudhakaran Jan 17 '17 at 11:54
  • 2
    @Arun Sudhakaran you can't. that's the whole point. – Mark Jan 17 '17 at 11:55
  • 1
    so can I say that **method-2** is an improper way of calling a non-static method? – Arun Sudhakaran Jan 17 '17 at 12:01
  • See here the difference:http://stackoverflow.com/questions/29740512/static-and-private-method-behavior-when-calling-direct-on-object-of-child-class – Sitansu Jan 17 '17 at 12:07

5 Answers5

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.

Piotr Podraza
  • 1,941
  • 1
  • 15
  • 26
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.

biziclop
  • 48,926
  • 12
  • 77
  • 104
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?

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
  • so in method-2 **new A()** is an instance as well as a variable too? – Arun Sudhakaran Jan 17 '17 at 12:25
  • @ArunSudhakaran, `new A()` is an instance, but not a variable (which just refers to a place where the instance lays) – Andrew Tobilko Jan 17 '17 at 12:31
  • 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? – Arun Sudhakaran Jan 17 '17 at 12:42
  • 1
    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 reference – Andrew Tobilko Jan 17 '17 at 12:52
  • @ArunSudhakaran, yes, there are no references to this object – Andrew Tobilko Jan 17 '17 at 12:55
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.

Yousaf
  • 27,861
  • 6
  • 44
  • 69
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.

Ravi B
  • 1,574
  • 2
  • 14
  • 31