1

I'm trying to make a simple script that will return the velue of an enum, let's just give an example: //EG.class (should return the Animal ID.

import Object.Animal;

public class EG {
    public void main() {
        Animal AnimalID = Object.Animal.CAT;

        System.out.print(AnimalID);
        //Should return value of CAT: 2000 (long)
        //But I can't figure out what's wrong.
    }
}

//Object.class

public class Object {
    public enum Animal {
        CAT(2000L), DOG(2001L), MONKEY(2002L), TIGER(2003L);

        private long animal;

        private Animal(long a) {
          animal = a;
        }

        public long getAnimal() {
          return animal;
        }
    }
}
2

2 Answers 2

10

You need to call System.out.print(AnimalID.getAnimal());

Sign up to request clarification or add additional context in comments.

Comments

6

Why not just create a toString() method on the Animal enum?

public enum Animal {
    CAT(2000L), DOG(2001L), MONKEY(2002L), TIGER(2003L);

    private long animal;

    private Animal(long a) {
      animal = a;
    }

    public long getAnimal() {
      return animal;
    }

    @Override
    public String toString() { 
       return this.name() + ": " +animal;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.