Enums - Getters and Setters

Discussion in 'Plugin Development' started by MrSparkzz, May 22, 2014.

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

    MrSparkzz

    I wanted to start using enums for global variables and I wanted to create clean getters and setters for variables, but I can't seem to find a way to do so.. Here's what I have so far
    Code:java
    1. public enum GlobalVariables {
    2. PLAYERSMAX(20);
    3.  
    4. private int i;
    5.  
    6. private GlobalVariables(int i) {
    7. this.i = i;
    8. }
    9.  
    10.  
    11. public static int getValue(GlobalVariables type) {
    12. return valueOf(type);
    13. }
    14.  
    15.  
    16. public void setValue(GlobalVariables type, int value) {
    17. // umm.. wut?
    18. }
    19. }
     
  2. Offline

    Garris0n

    Code:
    public int getValue(){
        return i;
    }
     
    public void setValue(int i){
        this.i = i;
    }
     
  3. Offline

    MrSparkzz

    Garris0n
    Yeah, but I wanted a method that was more simple than that. Because GlobalVariable.VARIABLE.getValue(); is just messy to me.
     
  4. MrSparkzz That's the standard way it works but if you insist...

    PHP:
    public enum GlobalVariables {
        
    MAX_PLAYERS(20);
     
        private 
    int i;
     
        private 
    GlobalVariables(int i) {
            
    this.i;
        }
     
        public 
    int getValue() {
            return 
    i;
        }
     
        public 
    void setValue(int i) {
            
    this.i;
        }
     
        public static 
    int getValue(GlobalVariable type) {
            return 
    type.getValue();
        }
     
        public static 
    void setValue(GlobalVariable typeint i) {
            
    type.setValue(i);
        }
    }
    Now try and tell me that GlobalVariables.getValue(GlobalVariables.MAX_PLAYERS); isn't more messy ;)

    Also, static imports.
     
  5. Offline

    SuperOmegaCow

    Why would you have a setter for a enum? It sort of defeats the purpose...
     
    Garris0n likes this.
  6. Offline

    Garris0n

    You would prefer
    Code:
    GlobalVariable.getValue(GlobalVariable.VARIABLE)
    to
    Code:
    GlobalVariable.VARIABLE.getValue()
    Uh...okay.
     
    oaschi likes this.
  7. Offline

    oaschi

    SuperOmegaCow
    If you want to store additional information it can be useful, for example:

    Code:
    enum Weekday{
      MONDAY("Montag"), TUESDAY("..."), WEDNESDAY("..."), THURSDAY("..."), FRIDAY("..."), SATURDAY("..."), SUNDAY("...");
     
      private String inGerman;
     
      Weekday(String inGerman){
        this.inGerman = inGerman;
      }
     
      //get-set
    }
    I'm glad this feature exists, I use it in a plugin I'm developing atm.
     
  8. oaschi Yes, but the code you've shown can do with a setter method and work exactly the same, yes? The point SuperOmegaCow is trying to make is that enums are constants - so why would you need to change what they represent? i.e. MONDAY is never going to change from Montag to something else, is it? So why do you need the setter?
     
  9. Offline

    oaschi

    AdamQpzm
    I should read more precisely, I thought he was asking why someone would make an instance variable in an enum.
    I agree that setters are not needed in enums.
     
    AdamQpzm likes this.
  10. Offline

    RainoBoy97

    Create a class with static fields.
     
Thread Status:
Not open for further replies.

Share This Page