mardi 17 novembre 2015

How get the name of a Integer variable in Java?

I have a class, that own multiple Integer. I need to get their name from the variable.

public class UserOptions {

    public UserOptions() {
    }

    int HELP, HAPTIC, REALTIME, RETRIEVE, ADMIN, FREE;

    public static String getName(Integer option) {
        for (Field field : UserOptions.class.getDeclaredFields()) {
            if (field.equals(option))
                return field.getName();
        }
        return "";

    }
}

Then when I want to use it with one UserOptions int variable (HELP, HAPTIC, REALTIME, RETRIEVE, ADMIN, FREE)

 String toto = UserOptions.getName(option);

Do not know how to do that, even by reading this post Java Reflection: How to get the name of a variable? : there is no field.get() ?!





Aucun commentaire:

Enregistrer un commentaire