1

I want to set objects equal to one and other, but when i do so i receive the following error:

"Exception in thread "main" java.lang.Error: Unresolved compilation problems: Duplicate local variable current Duplicate local variable h1 at objectx3Problem.mainmethod.main(mainmethod.java:15)"

Here is my source code:

public class mainmethod {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    human h1        = new human();
    human h2        = new human();
    human current   = new human();

    System.out.println(h1.getHealth());

    human current = h1; // error here
    current.DecreaseHealth();
    human h1 = current; //error here
    System.out.println("h1 has " + h1.getHealth() + "health");


    }

}

and

public class human {

    private int Health = 100;
    public int getHealth(){return Health;}
    public void setHealth(int Health){this.Health = Health;}

    public void DecreaseHealth()
    {
        Health = Health - 5;

    }

}

I have read the same question here setting objects equal to eachother (java),

but I do not understand how the top answer and my approach are different.

Thanks in advance

ziMtyth
  • 1,008
  • 16
  • 32
Jackie
  • 173
  • 9
  • You're trying to re-declare a variable that already exists. Did you mean to just use the existing one? `current = h1;` ? Or did you mean to create a new variable? In which case you'd want to give it a different name. – David Aug 20 '17 at 13:57

1 Answers1

4

The error is pretty straight forward:

You defined a variable here:

human current   = new human();

Then you try to create the same variable here:

human current = h1; // error here

The correct way is not use the type:

current = h1;

Same for the other variable.

Note make sure to read and properly use Java Naming Conventions

Jorge Campos
  • 22,647
  • 7
  • 56
  • 87
  • 1
    UPDATE: at the risk of depriving the community of your assistance, I will not delete this question. Thanks for the tip about naming conventions, that was also helpful to me. – Jackie Aug 20 '17 at 14:09