I'm currently writing a game in Java, and I'm trying to make a few simple design decisions that will reduce memory usage from the get go (to prevent from having to make large changes later to reduce memory usage). The main layout of the game will be a hex grid, and I'll be using a HashMap
to store the hexes in. Each entry is going to have a Point
object as the key, and a Hex
type object (Hex
is an interface) as the value. I'm going to have two different objects that implement Hex
; OceanHex
and LandHex
.
While LandHex
is going to contain a lot of fields (buildings, stats, players currently on the tile), players will not be able to build on or move onto the ocean tiles. Essentially, they're just aesthetic. For this reason, OceanHex
is only going to contain 2 static final members, making each instance of OceanHex
the same. I originally planned to cut down on memory usage by only instantiating one OceanHex
instance and having each ocean tile reference that instance, but this got me curious. Since OceanHex
only contains static fields, would many instances of OceanHex
actually use more memory than a single instance?
In general, my question is: for a class that only contains static fields, would having many instances of the object take up the same amount of space as having one instance of the object with many references to it?
I'd imagine creating instances of classes with only static fields is rare, but I'm curious nonetheless.