Solved Java get object

Discussion in 'Plugin Development' started by CraftBang, May 2, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    CraftBang

    Helldo Bukkiteers,

    I've got a question about java and I don't know if it's possible.
    Lets say I got 2 integers.
    int pricelevel1 = 50;
    int pricelevel2 = 100;

    Now I want to get those ints but I don't know how.
    Is there any possibilitie to do like:
    getInt("pricelevel1");
    And "pricelevel1" will refer to the int.

    One thing I know this is possible to create your own getInt function and create a switch and return the int. But is there another way to do this WITHOUT creating that function, does it already exists (in java) ?
     
  2. just use the name of them.
    For example:
    System.out.println(pricelevel1);

    Or if you want to get a variable from a String, you need to use reflections:
    Code:
    Object o = getClass().getDeclaredField("varName").get(this);
    System.out.println(o);
    or
    Code:
    int i = getClass().getDeclaredField("pricelevel1").get(this);
    System.out.println(i);
     
  3. Offline

    mine-care

    @CraftBang @FisheyLP Or to avoid hazards of reflection (See exceptions) you may also map the values (See java Maps)
     
  4. Offline

    CraftBang

    @FisheyLP exactly the getDeclaredField, thanks!
    And @mine-care what do you mean, like map them as Integer, String and then get the Integer using the String right?
    Mhmm aint that a waste of memory?
     
  5. Offline

    Freelix2000

    @FisheyLP
    I figured out your signature. The 'if' statement with all the weird Boolean stuffs will return "true", and the code "you.confused();" will run. The Boolean can be simplified first to "true != false", then "true".
     
  6. Offline

    mine-care

  7. Offline

    CraftBang

  8. But I wanted to make it confusing :oops:
     
  9. Offline

    mine-care

    @FisheyLP Hmm, you can try putting an encrypted message XD I plan doing that but I need some space in my signature. :- (
    Anyway pm me if you need further info :D
     
    CraftBang likes this.
Thread Status:
Not open for further replies.

Share This Page