Polymorphism. Problems with copy arrays

Empleado[] empleados = new Empleado[3];
empleados[0] = new Empleado("Alfredo", 20000, 2020, 6, 01);
empleados[1] = new Empleado("Alejandro", 21000, 2020, 7, 04);
empleados[2] = new Jefatura("Laura", 25000, 2010, 3, 6);
Jefatura jefeRRHH = (Jefatura) empleados[3]; // CASTEO
jefeRRHH.setIncentivo(1000);

Why is it that when i set the incentive for jefeRRHH, empleados[2] is changed? I thought if jefeRRHH was modified this didn't affect the array empleados.

I mean, when I walk the vector with a foreach loop, empleados[2] give me back with the incentive applied.

package poo;

public class test {

    public static void main(String[] args) {
        
        
        String Nombre = "José";
        int j = 1;
        
        String[] nombres = new String[1];
        nombres[0] = Nombre;
        
        int a = j;
        
        Nombre= "Maria";
        
        System.out.println(Nombre);
        System.out.println(nombres[0]);
        
        j=2;
        
        System.out.println(j);
        System.out.println(a);

    }

}

Why isn't the same thing here?

When I declare the new variables I'm not assigning addresses, I'm assigning copies, right?

Upvotes: 0

Views: 87

Answers (2)

gkhaos
gkhaos

Reputation: 705

The following will hold true for all Objects in Java.

Assuming a simple class:

public class Foo {

}

When you then instantiate an object of class Foo, you will actually allocate memory on heap, construct a Foo-Object and retrieve the adress to this location, which will then be stored in the object variable:

Foo f = new Foo(); // f points to a Foo-Object

So, instead of copying the actual object, the reference will be copied...

Foo farr[] = new Foo[1]; // create array with a capacity of 1
Foo f = new Foo(); // f now holds the adress to an actual Foo-Object
farr[0] = f; // farr[0] now holds the adress to the SAME Foo-Object

Here is a nice article about references and objects,
and here is a nice answer as follow-up-reading


Furthermore there are primitive datatypes and String-literals.

Primitive datatypes are always simply copied, whatever happens. So there will never be the case, where you change one primitive variable and another one changes.

Strings on the other hand are a bit trickier. They are objects wrapped around a char[], and therefore are (as already stated by others here) immutable and share a so called String constant pool. Modifying a String will therefore result in a new String with the result, and the original String remains unmodified.

Upvotes: 1

Alex Sveshnikov
Alex Sveshnikov

Reputation: 4329

You probably have a typo in the code, you should reference the element with index 2 here:

Jefatura jefeRRHH = (Jefatura) empleados[3]; // CASTEO

So, you create a new object with

new Jefatura("Laura", 25000, 2010, 3, 6);

and store a reference to it in the element of the empleados array with index 2. Then you say that jefeRRHH will be another reference to the same object. Now you have two references pointing at the same object. Think about them like different names (or nicknames if you like) of the same person. When you access/modify the object using any of the references you change the same object, so the changes are visible through any other names/references.

Upvotes: 0

Related Questions