Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call additional method in enums?

Tags:

java

enums

enum Enum1
{
    BIG(8), HUGE(10)
    {
        public String getName()
        {
            return "Huge";
        }

        public String getContry()
        {
            return "India";
        }//additional Method
    },
    OVERWHELMING(16)
    {
        public String getName()
        {
            return "OVERWHELMING";
        }
    };
    private int ounces;

    public int getOunes()
    {
        return ounces;
    }

    public String getName()
    {
        return "Ponds";
    }

    Enum1(int ounces1)
    {
        ounces = ounces1;
    }
}

class EnumAsInnerClass
{
    Enum1 enumInnerClass;

    public static void main(String[] args)
    {
        EnumAsInnerClass big = new EnumAsInnerClass();
        big.enumInnerClass = Enum1.BIG;
        EnumAsInnerClass over = new EnumAsInnerClass();
        over.enumInnerClass = Enum1.OVERWHELMING;
        EnumAsInnerClass huge = new EnumAsInnerClass();
        huge.enumInnerClass = Enum1.HUGE;
        System.out.println(big.enumInnerClass.getName());//Ponds        
        System.out.println(over.enumInnerClass.getName());//OVERWHELMING
        System.out.println(huge.enumInnerClass.getName());//Huge
    }
}

Consider the above example. How can I call the method getCountry for HUGE?

If there is no way to call this method, why does Java treat it as legal?

like image 257
ponds Avatar asked May 11 '11 06:05

ponds


People also ask

Can you add methods to enums?

You can use extension methods to add functionality specific to a particular enum type.

Can we add method in enum in Java?

The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared.

Can enums have instance methods?

Enums are very powerful as they may have instance variables, instance methods, and constructors. Each enum constant should be written in capital letters. Every enum constant is by default internally public static final of type Enum declared.

Can we write methods in enum?

An enum class can include methods and fields just like regular classes. When we create an enum class, the compiler will create instances (objects) of each enum constants. Also, all enum constant is always public static final by default.


2 Answers

(As noted nearly 4 years later, my original answer was incorrect.)

You can't even call Enum1.HUGE.getCountry() with the specific enum, which is very slightly surprising... but even if you could, you couldn't do so with an arbitrary Enum1 value, which would be more generally useful.

The solution is to stop it from being an additional method - add a getCountry() method into Enum1, either returning a default value or maybe throwing an exception. HUGE can then override the method.

It would be nice if you could declare that a particular enum value implemented an interface, but it seems there's no syntax for that.

like image 105
Jon Skeet Avatar answered Nov 15 '22 08:11

Jon Skeet


The enum is a type definition, similar to a class, so Enum1 doesn't have the method getCountry() available in its interface.

edit: Alternative solution

What you can do is define the required methods in Enum1 and override them in the individual enum values.

like image 36
Alan Escreet Avatar answered Nov 15 '22 07:11

Alan Escreet