1
\$\begingroup\$

I'm in a dilemma over using Enum Types or using class objects in that particular part of my java project.

Let's suppose I have an array where I constantly add and remove things from it, but I also render textures from that array via accessing the texture with an enum type from its class like:

public static Texture requestTexture(Type type){
    if(type == Type.One){
        return textureOne;
    }
}

// can be improved with hashmap

I wonder if I can use classes instead of enums? Will it slow down my performance since I will be constantly creating new objects and deleting them since they leave the stack, but on the other hand, it will be more cleaner and structured code?

\$\endgroup\$
  • \$\begingroup\$ It's unclear to me how classes would help here. Can you demonstrate your proposed class-based solution, and what specific problem you're having with it? Presumably you tried profiling your solution to evaluate its performance impact? \$\endgroup\$ – DMGregory Apr 27 at 13:48
  • \$\begingroup\$ Haha, liked your comment. I haven't developed the class-based solution, but I think the code will be more readable and structured, and independent by using polymorphism. I thought of the Design and lets suppose we have a Base class that has all the information we need(textures and other things). For every different inherited class we can get the current texture and it's class specified things. \$\endgroup\$ – vikAy Apr 27 at 13:59
1
\$\begingroup\$

Given that your proposed logic would typically look something like this...

public static Texture requestTexture(TextureType type)
{
    switch(type)
    {
        case TextureType.Undefined:
            return new Texture(); //class type used to create object
        break;
        case TextureType.Small:
            return new Texture(32,32); //class type used to create object
        break;
        case TextureType.Large:
            return new Texture(128, 128); //class type used to create object
        break;
    }
}

...you won't have a class instance (object) by the time you have to make this decision. This is what enum types are typically used for: rapidly switching on some minimal data item. That is exactly what you have to do here, in order to be able to construct from the associated class type.

Think of the enum as a phone book; the switch / case block as an old-fashioned telephone switchboard - and the object like the person you are trying to call; while the class is just a plan for creating that specific callee (your mom, your uncle, or your best friend).

enums are not at all the same as classes, which provide program logic; instead they are small data-related types used to define valid options. A common example of enum use is when you are asked to choose your country when signing up for webmail account; the entry is not the country, nor is the list of entries; instead it is like a telephone directory, representing a list of options in the real world. You just can't pick or enter a country that doesn't exist in that list.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Really nice answer, made my head up! \$\endgroup\$ – vikAy Apr 27 at 14:01
  • 1
    \$\begingroup\$ Enums in Java are surprisingly powerful. They actually can contain program logic. Take a look at the "Planet" example from docs.oracle.com/javase/tutorial/java/javaOO/enum.html \$\endgroup\$ – Philipp Apr 27 at 14:09
  • 1
    \$\begingroup\$ @Philipp True. I think we can argue that the pattern outlined above provides a useful distinction, and is why most languages implement the two as discrete concepts. I avoid focusing on corner cases. One can arguably do the same in Javascript or other, more dynamic languages. Does that make it useful in general? Probably not. Enums are typically needed on their own. I think what might be useful is having the enum value be the typeof / name of the class it's associated with. This would, in the OP's example, remove the conditional step, or a mapping serving the same purpose. \$\endgroup\$ – Engineer Apr 27 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.