If, let's say, you mapped 'space' to be int value 0, and 'wall' to be int value 1. In the code:
obj.setType(1);
it isn't immediately obvious what you're doing in this statement, unless you know what the mapping is. You should strive to make your code self-documenting. You could declare some static int's to use as constants, like so:
public static int SPACE = 0;
public static int WALL = 1;
and then you could do this:
obj.setType(WALL);
However, it still doesn't prevent you from doing something like this:
obj.setType(2);
because in your mapping, there's no such thing as 2. This would still be accepted by the compiler, if setType() was declared to take an argument of type int, however, you could have unforseen runtime errors unless you do some extra error checking to make sure that the int value is in the accepted range.
By instead using an enum type as I described above, the
compiler checks to make sure you're passing in an accepted value, because it won't accept any value other than what's written in the enum definition. Enum can be thought of as creating a new data type in the Java language, recognizable by the compiler. (In actuality, the entries are mapped to int's.)
With an enum, you can also do switch statements like this: (using the same enum example from my previous post)
switch (obj.getType()) {
case SPACE:
// draw a space on the screen.
break;
case WALL:
// draw a wall on the screen.
break;
}
You can also create EnumMaps, which are Hash Maps which use an Enum type as its key, which could be used, for example, for storing the image for a particular type:
EnumMap<GameObject.Type, Image> map = new EnumMap<GameObject.Type, Image>();
You may never need that, but you should be knowledgeable on what you are able to do with Enums.