Aside from the comparator issue (double-equals vs single-equals), there's a few other issues with your code that you can address that might clarify the role of getters and setters. You don't actually need to do any comparison in either of your methods.
If we declare that MOUNTAIN is always equal to 1 - then "1" and "MOUNTAIN" can be used interchangeably in our code. Let's replace all the digit literals (0, 1, 2) with the constant names instead.
public void setTerrain(int terrain){
if(terrain == DESERT){
this.terrain = DESERT;
}
if(terrain == MOUNTAIN){
this.terrain = MOUNTAIN;
}
if(terrain == FOREST){
this.terrain = FOREST;
}
}
This is a bit redundant. Actually, if we instead replace terrain types with numbers, the issue might be more apparent:
public void setTerrain(int terrain){
if(terrain == 0){
this.terrain = 0;
}
if(terrain == 1){
this.terrain = 1;
}
if(terrain == 2){
this.terrain = 2;
}
}
Imagine if you had 1000 terrain types - the code blows up even more.
public void setTerrain(int terrain){
if(terrain == 0){
this.terrain = 0;
}
if(terrain == 1){
this.terrain = 1;
}
if(terrain == 2){
this.terrain = 2;
}
if(terrain == 3){
this.terrain = 3;
}
...
if(terrain == 999){
this.terrain = 999;
}
if(terrain == 1000){
this.terrain = 1000;
}
}
Instead, you can just use something like this:
public void setTerrain(int terrain){
this.terrain = terrain;
}
As Dino's answer suggests, it is always a great idea to add Input Validation to handle cases where someone sends a number that isn't one of your supported terrains:
public void setTerrain(int terrain){
if (terrain < 0 || terrain > 2) {
System.out.println("Invalid terrain number!");
} else {
this.terrain = terrain;
}
}
Similarly, you can simplify your getter by just directly returning your Creature's terrain type - no comparisons needed!
public int getTerrain(){
return this.terrain;
}