How to get the class of a static object attribute



I have a parent class Tile and inherited classes Walltile, FloorTile, EntityTile. Each inherited class have BufferedImage attributes that is related with its name.

public class WallTile extends Tile {

public static BufferedImage WATER_TOP_LEFT = Game.tilesSpritesheet.getSprite(34, 0);
public static BufferedImage WATER_TOP = Game.tilesSpritesheet.getSprite(51, 0);
public static BufferedImage WATER_TOP_RIGHT = Game.tilesSpritesheet.getSprite(68, 0);
public static BufferedImage WATER_LEFT = Game.tilesSpritesheet.getSprite(34, 17);
public static BufferedImage WATER_CENTER = Game.tilesSpritesheet.getSprite(51, 17);
public static BufferedImage WATER_RIGHT = Game.tilesSpritesheet.getSprite(68, 17);
public static BufferedImage WATER_BOTTOM_LEFT = Game.tilesSpritesheet.getSprite(34, 34);
public static BufferedImage WATER_BOTTOM = Game.tilesSpritesheet.getSprite(51, 34);
public static BufferedImage WATER_BOTTOM_RIGHT = Game.tilesSpritesheet.getSprite(68, 34);
public static BufferedImage WATER_LITTLE_TOP_LEFT = Game.tilesSpritesheet.getSprite(17, 34);

public WallTile(BufferedImage sprite, int x, int y) {
    super(sprite, x, y);
}
}

public class FloorTile extends Tile {

public static BufferedImage TILE_GRASS = Game.tilesSpritesheet.getSprite(85, 0);
public static BufferedImage TILE_SAND = Game.tilesSpritesheet.getSprite(136, 0);

public FloorTile(BufferedImage sprite, int x, int y) {
    super(sprite, x, y);
}
}

public class EntityTile extends Tile {

public static BufferedImage TILE_TREE_1 = Game.tilesSpritesheet.getSprite(221, 153);
public static BufferedImage TILE_TALL_GRASS = Game.tilesSpritesheet.getSprite(374, 187);

public static List<BufferedImage> spriteList = new ArrayList<BufferedImage>();

// Construtor
public EntityTile(BufferedImage sprite, int x, int y) {
    super(sprite, x, y);
}

@Override
public void render(Graphics g) {
    g.drawImage(FloorTile.TILE_GRASS, x - Camera.x, y - Camera.y, null);
    g.drawImage(sprite, x - Camera.x, y - Camera.y, null);
}
}

I instantiate Tile and then i cast to the specific class, like:

Tile tile = new Tile(sprite, 0, 0);
WallTile wt = (WallTile) tile;

But i need to know if this Tile object is WallTile, FloorTile or EntityTile. For that i have the sprite attribute which corresponds to the ****Tile class i want. How can i get this info? When i use sprite.getClass() it returns BufferedImage, but i need return like WallTile, FloorTile or EntityTile. To do this:

if (tile.sprite.getClass().toString() == "class World.WallTile")
    //do something

Answer

This line of your code does not create a WallTile object.

Tile tile = new Tile(sprite, 0, 0);

The above line creates a Tile object and hence cannot be cast to WallTile.

I couldn’t find the code for class Tile in your question but I guess that you should make it an abstract class and give the constructor protected access so as to avoid directly constructing Tile objects.

Consider class Furniture and its subclass Table. I can create a Table but I can’t create a Furniture because Furniture is a general term. Its purpose is to store common attributes from all types of furniture.

Similarly, your Tile class is a base class that simply stores attributes that are common to WallTile, FloorTile and EntityTile.

The following line of code will create a WallTile as a Tile object and hence can be cast.

Tile tile = new WallTile(sprite, 0, 0); // 'tile' is actually 'WallTile' instance
WallTile wallTile = (WallTile) tile;

The sprite attribute of class Tile is a BufferedImage, regardless of the type of object that it is an attribute of. So the class of sprite will always be BufferedImage. What you can do is check what sprite it actually is, since all possible values for sprite must be one of the constants that you defined.

So in order to determine whether a Tile object is actually a WallTile, for example, you could do something like the following:

if (sprite == WallTile.WATER_TOP_LEFT ||
    sprite == WallTile.WATER_TOP      ||
    // etc) {
}

Still, I’m wondering why you need to do this. You didn’t post a minimal, reproducible example so the context is missing. I don’t know how you obtain a Tile object and why you need to determine the actual type of Tile it is. Nonetheless, can you not simply do the following?

if (tile instanceof WallTile)


Source: stackoverflow