java – Item material storage type


Let's say there is a class Item – subject
And this class has a variable that stores the value – the type of the item
It could be a table, or a key, or something else.
And now there is a need to know what kind of object is in front of us? It’s immediately clear to everyone, you need to write a getter
But it’s not clear to me: is it better to store in a variable of an integer type or a string?
For example(Java):

private final int ID;
/* куча кода */
ID = 255;


private final String TYPE;
/* еще одна куча кода*/
TYPE = "Stone";

PS: The sample code is written in Java, but it is not specific to this language


Do not store the type (class) of an object in an object if you are dealing with a language with strong typing. Typically, this is a design error. You can easily google the Russian translation of the book "Slippery Places in C++", which describes in detail why this should not be done – except in very rare cases – in short: overhead, extra code, manual checks, a mountain of errors.

For other languages, I think similar articles/tutorials are also easy to find.

Understand that the type of an object and its value are two completely different concepts.

Polymorphism was invented just so that the programmer does not choose, for example, "what function to call if the object must move?"

  • Stone – does not move
  • Bird – flying
  • Chicken – (supposedly not a bird), walks. but if she is frightened, she jumps up and flies a little, and almost always flies if she jumps from a height.
  • cat – walks
  • Car rides
  • Mine with motion detector – explodes

in a language with strong typing and support for polymorphism, you just call the move method and the object executes its own move method, and if it does not have such a method, you (may) get a compile-time error. No extra checks, no extra errors

PS besides, C++ and Java have their own ways to find out the type of an object:

  • for C++ – RTTI at runtime or SFINAE at compile time
  • for Java – someObject.getClass()

for Java, I guess the call to getClass can incur additional runtime overhead, if you also start storing the type as a separate value, the costs (memory / cpu) will increase even more

for C++, using RTTI always incurs additional costs (and a bunch of other problems), and is rarely used

PPS If we talk about metaprogramming, then in C ++ using templates / libraries in the spirit of boost: hana, the concepts of type / value can be slightly blurred, but this is a completely different topic for conversation

PPPS if you feel itchy and / or really need it (for example, in the case when your materials are not some classes, but just numbers of textures), then they should be stored in constants available to the language (for c ++ this is define / enum / enum class ), not in string values.

Constants will be processed at compile time, take up less memory space, and besides, you will get autocompletion support for your IDE (because it will see that the MATERIAL_TYPE_CONCRETE constant (aka concrete, let's say) is present in the code), and the lines will dangle back and forth with with all their constructors and overheads and without any support from the IDE

PPPPS is not specific to the question, but it can help you: if you are making a game or something similar that requires a lot of different resources, textures, object models, etc., I would do the following if I were you:

  • push all the resources into the database (mysql/postgres/sqlite – look at the situation) let's say in the form of a record (id, name, resource type, object hash can be)

  • make a simple web editor or command script to add a resource to the database.

  • write a simple script that generates a C++ header file or a Java class for you from this database

it will take a day or two of work, but you will start to feel the profit every time you do not have to manually add a file to the code, place it in the right directory, etc.

if for the debug mode you also add an entry to the database by the frequency of using the resource, you can, for example, get statistics with a simple request, which resources you do not use, or for which resources there are too few textures, etc.

Scroll to Top