Error when trying to set objects equal to one and other (java, beginner) -
i want set objects equal 1 , other, when receive following error:
"exception in thread "main" java.lang.error: unresolved compilation problems: duplicate local variable current duplicate local variable h1 @ objectx3problem.mainmethod.main(mainmethod.java:15)"
here 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 same question here setting objects equal eachother (java),
but not understand how top answer , approach different.
thanks in advance
the error pretty straight forward:
you defined variable here:
human current = new human();
then try create same variable here:
human current = h1; // error here
the correct way not use type:
current = h1;
same other variable.
note make sure read , use java naming conventions
Comments
Post a Comment