Your mistake is that your constructor's parameter names are the same as your class variable names. Java looks at variables with the closest scope. So when you say:
You are assigning the parameter variable to itself. You are NOT modifying the class variable. To modify the class variable, use the "this" keyword:
Also, fun fact: since you are using Enums in your getSpeed() method, you can actually clean it up quite nicely using something called a switch statement.
Here is a comparison of your code:
public int getSpeed(CharacterRace charRace)
{
int total=0;
if(charRace==CharacterRace.HUMAN){
total=speedFast;
}else if(charRace==CharacterRace.ELF){
total=speedFast;
}else if(charRace==CharacterRace.HALFELF){
total=speedFast;
}else if(charRace==CharacterRace.HALFORC){
total=speedFast;
}else if(charRace==CharacterRace.DWARVE){
total=speedSlow;
}else if(charRace==CharacterRace.GNOME){
total=speedSlow;
}else if(charRace==CharacterRace.HALFLING){
total=speedSlow;
}
return total;
}
verse a switch statement which produces the same result:
public int getSpeed(CharacterRace charRace) {
int total = 0;
switch(charRace) {
case HUMAN:
case ELF:
case HALFELF:
case HALFORC:
total = speedFast;
break;
case DWARVE:
case GNOME:
case HALFLING:
total = speedSlow;
break;
}
return total;
}
The main advantage to the switch statement is that it allows you to compress all of those if/else clauses into a quick and easy-to-read list of condition statements.