vinayaka
vinayaka

Reputation: 39

Initializing the constants in an enum with values from a property file

I have an enum in one of my project and I have used my constants here. However, I want to initialize these constants with values from a property file. How can I do this?

Upvotes: 1

Views: 2647

Answers (3)

halil
halil

Reputation: 800

I dont have compiler with me, but if I understand correctly what you want is something similar to this:

public enum YourEnum{
    VALUE("default");

    private String name;

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

    static{
        //below line you must implement your own property reading style from your file
        YourEnum.VALUE.name = FileUtil.readFromYourFile("propertyName");
    }
}

Hope, it helps, I'll have a look and correct if there are any compile errors when I get the chance to compile.

Upvotes: 0

Abacus
Abacus

Reputation: 19451

Enums are created at compile time, so no chance to change them at runtime.

Upvotes: 2

Jakub Macina
Jakub Macina

Reputation: 982

If you know the number of enum values, you could use Enum with values to insert values from property file.

In other case, it is not possible to populate enum at execution time, see this SO thread

Upvotes: 0

Related Questions