Why do not you have to instantiate the list to not accumulate?

2

I have the following class:

public class TesteLista {

 public static void main(String[] args) {
    List<String> dados;

    Teste1 t = new Teste1();
    dados = t.getList();

    System.out.println(dados);
 }
}

Class that has method:

public class Teste1 {

public List<String> getList(){
    List<String> lista = new ArrayList<>();
    lista.add("Aline");
    lista.add("João");
    lista.add("Jona");
    lista.add("Pedro");

        return lista;
 }
}

It prints:

  

[Aline, João, Jona, Pedro]

Why does not the data list accumulate since I do not give new ? Why do not I need to give dados= new ArrayList<>(); to not accumulate?

    
asked by anonymous 26.09.2017 / 20:12

2 answers

6

What you're doing is a copy of references of a type List between variables. So you do not need to instantiate, since you are already assigning an element that has already been instantiated and copying its reference to the dados variable.

Imagine your code looks like this:

List<String> dados;

...

List<String> lista = new ArrayList<>();
lista.add("Aline");
lista.add("João");
lista.add("Jona");
lista.add("Pedro");

...

dados = lista;

That's more or less what's happening. The variable dados is nula when started, but assigning it the list reference created within its Teste01 class, it starts to "point" to that list.

    
26.09.2017 / 20:29
4

I think your interpretation of the code is wrong. Probably not thinking about how it runs, I imagine you think it puts words and magically something happens. Everything that is written has a consequence and you have to understand the whole process, otherwise you will not learn to program.

Let's understand what happens with the list:

On line

List<String> lista = new ArrayList<>();

A list of strings is created and is referenced by a variable called lista .

On the following lines

lista.add("Aline");
lista.add("João");
lista.add("Jona");
lista.add("Pedro");

elements are added to this list.

Finally on the line

return lista;

The list is returned to the caller. The variable lista no longer exists, but the object pointed to by it, in case the list with 4 elements still exists and will be pointed by the one who called this method.

Now we return to the main method. In the line

Teste1 t = new Teste1();

An object of type Teste1 is created and placed in the variable 't'. Actually there would not have to be an object just for this, but come on.

On line

dados = t.getList();

The method is called and its return, that our list is passed to the variable data that had already been declared. So now dados passes the list that was in the lista variable inside the getList() method.

Finally the line

System.out.println(dados);

prints the list.

You do not need another new anywhere because you only need to create the list once. There is nothing to "accumulate" because the code does not have to do this. If he had it done then he would. It would even be impossible to have [Aline, João, Jona, Pedro Aline, João, Jona, Pedro, Aline, João, Jona, Pedro] because only a list with only 4 elements was created.

You need to understand what the code is doing but keep repeating ready codes and you do not know what you're doing.

I even imagine you want to do other things, but the question does not even talk about it. Before programming it is necessary to understand the problem and express it in Portuguese. If this can not be done, it is certain that in Java it will not be right, since the base is wrong.

    
26.09.2017 / 20:32