Different methods of creating an object

Asked

Viewed 364 times

6

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

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

    new Usuario().listar();

4 answers

6


Generally speaking it’s the same thing.

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

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

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

Just remembering that not being accessible does not mean that the object will be destroyed immediately, only when the Java garbage collector goes into action will it be destroyed.

I think it’s worth remembering that a variable is just a name you give to a memory address. Through it you can refer to this memory address in an easy and readable way in the 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 stores nothing and the object is "loose" and no later access precisely because it does not have a name to reference.

There are cases where if the second is necessary it might be the case that the method is static, but I cannot say that this case is not concrete.

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

4

Create an object like this:

new Usuario();

It implies not keeping his reference in 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();

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

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 have no way to reference the object in the next line of your code.

About calling the list method, I think the code (without variable) is more readable like this:

(new Usuario()).listar();

This clearly shows a person who is reading your code that you are first creating an object of the type Usuario and soon after called the method listar of that created object.

3

Usuario usuario = new Usuario();

You are creating an object Usuario and assigning 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() - but is not assigning the created object to a variable, after this line the object is inaccessible.

What difference does the object be accessible or inaccessible?

Every inaccessible object is considered eligible for Garbage Collector.

2

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.