Different methods of creating an object

3

What is the difference between these two types of creation / instantiation of an object?

Usuario usuario = new Usuario();
    usuario.listar();

    new Usuario().listar();
    
asked by anonymous 22.10.2016 / 22:45

4 answers

5

Generally speaking it's the same thing.

Usuario usuario = new Usuario();
usuario.listar();

Obviously the first is stored in a usuario variable and the object can be used for other things. The listar() method call occurs on top of the reference stored in usuario .

In the second the object is created in the new Usuario() part, it calls the desired method (based on the newly created object), and it is no longer accessible in your code soon after, as there will be no references for him.

Just remembering that not being accessible does not mean that the object will be destroyed immediately, only when the Java garbage collector kicks in is that it will be destroyed.

I think you should remember that a variable is just a name given to a memory address. Through it you can refer to this memory address easily and legibly in code, the compiler turns to keep a correct reference. Without the variable the object that is in a memory address is not controlled by the application, the compiler does not store anything and the object becomes "loose" and without later access precisely because it does not have a name to reference.

There are cases that if the second is necessary it might be the case that the method is static, but I can not speak of this case that is not concrete.

The functional example in ideone and in CodingGround .

    
22.10.2016 / 23:03
3
Usuario usuario = new Usuario();

You are creating a Usuario object and assigning it to a variable. After this line the object is accessible by the reference usuario .

new Usuario().listar();

You are creating an object in the same way, and using a method of this object, - in this case, maybe the method could be static , then you would use Usuario.listar() - however you are not assigning the created object to a variable, after this line the object is inaccessible.

What difference does the object make accessible or inaccessible?

Any inaccessible object is considered eligible for the garbage collector.

    
22.10.2016 / 23:03
3

Create an object like this:

new Usuario();

It means not saving its reference to a variable. This can be done, for example, when you just need to instantiate, call a method, not needing the object afterwards.

On the other hand, do so:

Usuario usuario = new Usuario();

It involves saving the reference (read memory address) of the object created in the usuario variable.

In the end, the effect of instantiating an object is the same in both situations. The only difference, as stated, is that in the first case you can not reference the object on the next line of your code.

On calling the list method, I believe the code (without variable) becomes more readable like this:

(new Usuario()).listar();

This clearly shows to a person reading your code that you are first creating an object of type Usuario and then calling the listar method of that created object.

    
23.10.2016 / 13:16
2

Briefly: an implementation stores the object reference in a variable. With this, you can use this object at another point in the algorithm. The second implementation generates only one user instance to execute the method list and does not save the reference in a variable, making it impossible to use it in other points of the algorithm.

    
23.10.2016 / 13:41