Assigning and updating static variable in java

1

I have a problem with a Static variable. In the code I assign to a variable that is not static, the contents of a Static variable. Then I update the static variable, but the variable that received the value of static there at the beginning is updating together and I do not understand why this is happening. Type, I want to save the value of the static variable to another variable at the beginning of the code, so I can use it later. But this other variable is updating together and I do not understand why.

public class Jogador {
    public static Heroi jogador;   
    public static Item armaEquipada;
    public static Item defesaEquipada;
    public static Personagem oponente;
    public static int IdSala;

    public Jogador(){
        jogador = new Heroi("Optimus", 10, 100, 50, 100);
        oponente = new Vilao("", 0, 0);

    }
}

Here are the variable types:

public Heroi EstadoAnteriorHeroi = null;
public Personagem EstadoAnteriorVilao;

Here are the assignments:

EstadoAnteriorVilao = Jogador.oponente; 
EstadoAnteriorHeroi = Jogador.jogador; // guarda os atributos anteriores do heroi se ele morrer e ainda tiver vidas;
System.out.println("Nome vilao: "+EstadoAnteriorVilao.getEnergia());

Then I update the Attributes of the Player.oponente only that in some way I do not understand EstadoAnteriorVilao receives the same changes.

If you can help me understand what happens.

    
asked by anonymous 15.08.2017 / 20:56

1 answer

0

Search for Cloneable

Example:

public class Jogador implements Cloneable {

Jogador getClone() {
    try {
        return (Jogador) super.clone();
    } catch (CloneNotSupportedException e) {
        return this;
    }
}

}

    
17.08.2017 / 18:46