Java enum getter -


i appreciate here.

executing following code, buyer2 id override buyers1 id. means id=2. not sure wrong following code. think enum , method retains last value.

buyer bone = new buyer("buyer1", 1);  buyer btwo = new buyer("buyer2", 2); 

rest of code:

public enum fruits {     banana("banana", "b"),     apple("apple","a"),     orange("orange","o");      private string type, id;      private fruits(string type, string id){         this.type = type;         this.id = id;     }      public string gettype() {         return type;     }      public string getid() {         return id;     }      public void setid(string id){         this.id = id;     } } 

public class player {     private string name;     private fruits banana, apple, orange;      private int id;      public buyer(string name, int id) {         this.name = name;         this.id = id;         banana = fruits.orange;         apple = fruits.apple;         orange = fruits.orange;          banana.setid("b"+id);         apple.setid("a"+id);         orange.setid="o"+id;     } } 

thanks

your fruits enum mutable, not want. setid() method on fruits design error. different fruits (banana, apple, ...) single instance, shared through program, need reflect globabl state, not state specific buyer.

try remove setter , see if can refactor code ...


Comments

Popular posts from this blog

python - Installing PyDev in eclipse is failed -

PHP OOP-based login system -

c# - Nested Internal Class with Readonly Hashtable throws Null ref exception.. on assignment -