Q: java classes and loop for inserting names I'm facing some problems with java classes.
Here is a class I defined
public class Condutor {
    private String name;

    public Condutor() {
        name = ""; 
    }

    public Condutor(String aName) {
        this.name = aName;
    }

    public void setName(String aName) {
        name = aName;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return name;
    }

}

and on the main I have this
public static void main(String[] args) {
    Condutor[] names = new Condutor[10];
    Condutor name = new Condutor();
    String aName = "";

    for (int i = 0; i < 2; i++) {
        aName = "Mike" + i;

        name = new Condutor(aName);
        names[i] = name;      
    }

    for (int i = 0; i < 10;i++) {
        System.out.println(names[i]);
    }

} 

This works as expected printing this:
    Mike0
    Mike1
    null
    null
    null
    null
    null
    null
    null
    null
Maintaining the same class but changing
name = new Condutor(aName);
names[i] = name;

by
name.setName(aName);
names[i] = name;
System.out.println("------");
System.out.println(names[i]);
System.out.println("------");   

it prints
------
Mike0
------
------
Mike1
------
Mike1
Mike1
null
null
null
null
null
null
null
null

I can see that in the loop everything works as expected but the names array stores in the first two positions the last input.
Why does this happens?
Should't it store exactly as in the loop?
regards,
Favolas

A: This happens because only one Condutor is ever created and it is added at both the [0] and [1] locations.
For i = 1 when you change the name it is reflected at both [0] and [1] location

A: Because in the second you are not creating a new instance of Condutor. You are using the same instance each time.
Condutor[] names = new Condutor[10];
Condutor name = new Condutor(); // this is the one that is being used.


A: You need to create new objects in the second case too:
name = new Condutor(); //add this line to make sure you create new object in every iteration
name.setName(aName);
names[i] = name;
System.out.println("------");
System.out.println(names[i]);
System.out.println("------");


A: In your 1st version, you are creating a separate instance in each iteration of the first loop. This way, every "condutor" keeps his individual name.
In the 2nd version however, you are assigning and therefore reusing the same Condutor instance (the one created in line 2 of main()) over and over in each iteration, only changing the name of this one object. Thus, when printing the results, you only see the name of this single object, how it was set last.

A: When you do
names[i] = name;

you're not copying the object under name, but you're simply copying a reference to it into names[i]. Note that after the change you never create any instances of Conductor other than the one created on the second line of main. Note also that name always refers to the same instance you created in the second line of main. It follows that when you do
name.setName(aName);

you repeatedly set the name of the same object. You also copy the reference (not the object) to the first and second elements of the array:
names[i] = name;

Eventually, there is only one Conductor which has had its name updated twice (in the first loop) and reference to which is stored in name, names[0] and names[1]. This explains the output.
