0
final static int 
    DRAGON = 5,
    SNAKE = 6;

String this_item = "DRAGON";
int item_value = super_func(this_item);//must be 5

Is it possible to implement this super_func, as shown above?

Or maybe I am asking for too much?

EDIT:

I am not allowed to use enum. I can:

a) use a map (as someone pointed, or 2 array lists) or
b) do this with some pro way (but it doesn't seem possible).

Roman C
  • 49,761
  • 33
  • 66
  • 176
Luka
  • 1,761
  • 2
  • 19
  • 30

5 Answers5

3

I don't know what this value is supposed to represent, but you could use an Enum.

enum Animal {
    DRAGON(5),
    SNAKE(6);

    private final int a;

    private Animal(int a){
        this.a = a;
    }

    public int getA(){
        return a;
    }
}

And then

String this_item = "DRAGON";
int item_value = Animal.valueOf(this_item).getA();//5
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
2

You can get the value of a variable with Java Reflection tricks, but why not declaring a simple enum and using it:

    enum Creature{
    DRAGON(5), SNAKE(6);
     ...
    }
Juvanis
  • 25,802
  • 5
  • 69
  • 87
1

As far as I know such a thing is not possible, but you coudl store your variables in Map

HashMap<String,Integer> map = new HashMap<>();
map.put("DRAGON",5);
map.put("SNAKE",6);

int itemValue = map.get("DRAGON"); //Sets itemValue to 5
HelloWorld123456789
  • 5,299
  • 3
  • 23
  • 33
wastl
  • 2,643
  • 14
  • 27
1

You can also use a switch() statement.

int item_value;
switch(this_item) {
     case "DRAGON":
         item_value = 5;
         break;
     case "SNAKE":
         item_value = 6
         break;

or different solution doing the opposite

private String getAnimal(int itemValue) {
     switch(item_value) {
         case 5:
             return "DRAGON";
         case 6:
             return "SNAKE";
         default:
             return null;
}
Cobbles
  • 1,748
  • 17
  • 33
1

While it's technically possible to do this using reflection, I'd strongly recommend you to rethink your structure, and consider one of the alternatives that have been mentioned in the other answers.

The simplest would probably be to use a map

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

public class VariableNameTest
{
    private static final Map<String,Integer> map;
    static
    {
        Map<String, Integer> m = new LinkedHashMap<String, Integer>();        
        m.put("DRAGON",5);
        m.put("SNAKE",6);
        map = Collections.unmodifiableMap(m);
    }

    public static void main(String[] args)
    {
        System.out.println(getValue("DRAGON"));
        System.out.println(getValue("SNAKE"));
        System.out.println(getValue("Boo!"));
    }

    public static int getValue(String name)
    {
        Integer i = map.get(name);
        if (i == null)
        {
            // Do some error handling here!
        }
        return i;
    }
}

But if your intention is to extend this with additional functionality, you should probably introduce a class like it was recommended in https://stackoverflow.com/a/23846279/3182664

Just for completeness: The reflection solution, not recommended!

import java.lang.reflect.Field;

public class VariableNameTest
{
    static final int DRAGON = 5;
    static final int SNAKE = 6;

    public static void main(String[] args)
    {
        System.out.println(getValue("DRAGON"));
        System.out.println(getValue("SNAKE"));
        System.out.println(getValue("Boo!"));
    }

    private static int getValue(String name)
    {
        try
        {
            Class<?> c = VariableNameTest.class;
            Field f = c.getDeclaredField(name);
            return f.getInt(null);
        }
        catch (NoSuchFieldException e)
        {
            // Many things
            e.printStackTrace();
        }
        catch (SecurityException e)
        {
            // may cause
            e.printStackTrace();
        }
        catch (IllegalArgumentException e)
        {
            // this to 
            e.printStackTrace();
        }
        catch (IllegalAccessException e)
        {
            // fail horribly 
            e.printStackTrace();
        }
        return 0;
    }
}
Community
  • 1
  • 1
Marco13
  • 53,703
  • 9
  • 80
  • 159
  • Not recommended, because it's too complex and there are simpler alternatives, right? – Luka May 24 '14 at 14:51
  • Not recommended, because `Many things` `may cause` `this to` `fail horribly` - and there are simpler, less invasive solutions, yes. – Marco13 May 24 '14 at 14:53
  • He said he's not allowed to use enums – Cobbles May 24 '14 at 15:06
  • @Cobbles This "edit" of the original question was done while (or shortly after?) I referred to ZouZou's recommendation. However, I intentionally said that he might introduce a *class* (which does not necessarily mean that this class should extend the `Enum` class). Such a class could offer a functionality that is *roughly* similar to that of an `enum`. Of course, one could not `switch` over the instances/values of this class, but a `valueOf(String)` method could be offered, solving the initial issue. – Marco13 May 24 '14 at 15:11
  • @Marco13 Ah I understand, sorry for accusing you :P – Cobbles May 24 '14 at 15:14