When to reference and when to instantiate

3

I have a question, what is the difference between these 2 ways of storing an object? In this first form I create an object and assign it to another one that is the result of a search in the database:

User user = new User();
user = userService.findById(idUser);

and this other way:

User user = userService.findById(idUser);

is about what is POO and what is instantiate and all that, but I do not understand what would be the difference between creating an object and assigning a value to reference an object (as a type) and assign a value, to my second opinion would be the best option but I do not know if I am correct, if someone could explain it to me I would appreciate it. Thanks.

    
asked by Hugo Valenza M 23.02.2018 в 16:40
source

2 answers

4

The first form is incorrect because it is a waste of resources (time and memory).

Let's analyze it:

User user = new User();

In that line 3 operations are made:

  • A variable of type User is declared.
  • An instance of the User class is created, calling the constructor.
  • The newly created instance is assigned to the variable. That is, the user variable points to an object of the User class.
  • user = userService.findById(idUser);
    

    The following occurs in this line:

  • A method is called which, given an identifier, returns an instance of the User class.
  • Once the instance is obtained, it is stored in the user variable. If the variable user was already pointing to another instance, the new one replaces the old one. If the old one is not being pointed by any other variable (which is the case, in this case), it will be destroyed by the garbage collector.
  • So the first line does things that are not necessary.

    In the second case we have

    User user = userService.findById(idUser);
    

    Which is more efficient: a variable of type User is created and assigned the instance that the method findById generates and returns.

        
    answered by 23.02.2018 / 17:40
    source
    8

    Let's see .. this is a concept problem

    Every variable (and I am going to speak almost agnostically) is a pointer (if the language allows it) or is directly the value.

    In this case, in java, that variable that you define, when declaring it of type object (because user secure is a class, and every class inherits type object) is a pointer. The problem is that beyond knowing what kind of pointer it is, you need to tell who it is pointing to.

    Your first version:

    User user = new User();
    user = userService.findById(idUser);
    

    He says:

    Crea la variable user de tipo User, y apuntalo a una nueva instancia de la clase User
    Ahora agarra la variable user y hacela apuntar al objeto User que devuelve userService.findById(idUser)
    

    That is, you are creating an object in memory that you will not use, and then assigning it another value.

    Obviously, all this works .. you spent memory space to create that object (and the pointer) and you did not use it, but in the end the GC (garbage collector) is going to rescue it.

    The second version of your code, does something more logical ...

    You want the variable .. and you know what it is going to have .. then what better way to pass it directly to that?

        
    answered by 23.02.2018 в 17:32