1

I need to define an Enum type in Java. However, the values in the enum depends on a parameter.

public class EnumTest {

    private Language lang;

    public enum Language {English, Spanish, Chinese, German, Japanese;}

    public enum Entity {

        // ??? 
        entityA("student"),
        entityB("faculty"),
        entityC("staff");

        private String name;

        Entity(String name) {
            this.name = name;
        }
    }

    public EnumTest(Language lang) {
        this.lang = lang;
    }

    public static void main(String[] args) {
        EnumTest testA = new EnumTest(Langauge.English);
        EnumTest testB = new EnumTest(Language.Spanish);
    }
}

For example, the instantiation of entityA, entityB, entityC will not be "student", "faculty", and "staff", if the parameter is not Langauge.English. It will be the corresponding words translated from English in other languages. So, the definition of the enum Entity depends on the parameter Lang.

How can I achieve?

4
  • 1
    This is analagous to having variable or class names change depending on runtime conditions, which makes no sense. Commented Jul 6, 2015 at 21:52
  • 9
    Enum values are compile-time constants. (If you did create a run-time enum the values could not be used in previously compiled code so that is largely irrelevant.) Commented Jul 6, 2015 at 21:52
  • 2
    Can you not just leave the enum constants' names in English in you code, and then translate them when they are displayed to the user? I'm not sure for what purpose you're using the enum, so I can give you exact details. Commented Jul 6, 2015 at 21:58
  • 3
    What it sounds like you want is a resource bundle, not an enum. Commented Jul 6, 2015 at 22:06

2 Answers 2

1

You could store the different translations within a field inside the enum.

public enum Entity {

    EntityA("student", "translation1", "translation2", "translation3", "translation4"),
    EntityB("faculty", "translation1", "translation2", "translation3", "translation4"),
    EntityC("staff", "translation1", "translation2", "translation3", "translation4");

    public String[] names;

    private Entity(String english, String spanish, String chinese, String german, String japanese) {
        names = new String[]{english, spanish, chinese, german, japanese};
    }
}

You could then get the appropriate translation with a method like the following:

public String getTranslation(Entity entity, Language lang){
    return entity.names[lang.ordinal()]; // ordinal returns the id of the enum instance. The first declared has 0, the second has 1 and so on
}

So, the below code

getTranslation(Entity.EntityA, Language.Spanish)

would return "translation1".

Sign up to request clarification or add additional context in comments.

Comments

1

The major advantage of enum types in Java is the ability for the compiler to know and have access to all instances at compile time. To define or modify enum values at runtime, even in tests, is to undermine Java enums and the guarantees and optimizations they support (e.g. serialization, VM-wide singleton properties, and well-defined ordinal and values[]).

A few alternatives you might consider:

  • Keep Entity as a enum, and shortly after runtime load a long-lived Map<Entity, String> that keeps the localized name.

  • Limit access to the "Entity" constructor (to make the class instance-controlled) and then provide instances through a method or series of methods.

  • Have all localized strings on the enum take some sort of table instance (e.g. Properties or Map) that it uses to extract the right name based on an externally-kept key.

4 Comments

The OP is not trying to create new enum instances, but to retrieve a translation depending on an enum value. The OP's aim was badly explained in the question, but if you look at the code, you will realise.
@SamTebbs33 Acknowledged and rephrased. The point stands, though—making an enum or its instances mutable is asking for trouble. (Also, future readers may find this question who see the title and expect the broader scope.)
Yes, you're definitely right about making an enum's fields mutable is bad practice, especially since they are intended to be constant thorughout the lifetime of an application.
Thanks all. I will digest all the possibilities. It's not a good title. The code makes the desire clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.