Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enums: methods exclusive to each one of the instances

Tags:

From another question I have learnt that it is possible in Java to define specific methods for each one of the instances of an Enum:

public class AClass {      private enum MyEnum{         A { public String method1(){ return null; } },         B { public Object method2(String s){ return null; } },         C { public void method3(){ return null; } } ;     }      ... } 

I was surprised that this is even possible, do this "exclusive methods" specific to each instance have a name to look for documentation?

Also, how is it supposed to be used? Because the next is not compiling:

    private void myMethod () {         MyEnum.A.method1();     } 

How am I supposed to use these "exclusive" methods?

like image 551
Mr.Eddart Avatar asked Jul 01 '11 18:07

Mr.Eddart


People also ask

Can enums have methods?

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 user defined methods?

Below code uses enums with defined methods: We should define methods as abstract methods and then we have to implement defferent flavours/logic based on each enum members. Because of declaring abstract method at the enum level; all of the enum members require to implement the method.

Do enums have instances?

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 add methods in enum Java?

In Java (from 1.5), enums are represented using enum data type. Java enums are more powerful than C/C++ enums. In Java, we can also add variables, methods, and constructors to it. The main objective of enum is to define our own data types(Enumerated Data Types).


2 Answers

You need to declare abstract methods in your enum which are then implemented in specific enum instances.

class Outer {      private enum MyEnum {         X {             public void calc(Outer o) {                 // do something             }         },         Y {             public void calc(Outer o) {                 // do something different                 // this code not necessarily the same as X above             }         },         Z {             public void calc(Outer o) {                 // do something again different                 // this code not necessarily the same as X or Y above             }         };          // abstract method         abstract void calc(Outer o);     }      public void doCalc() {         for (MyEnum item : MyEnum.values()) {             item.calc(this);         }     } } 
like image 123
Sanjay T. Sharma Avatar answered Nov 06 '22 02:11

Sanjay T. Sharma


You cannot refer to those methods, because you are effectively creating anonymous (*) class for each enum. As it is anonymous, you can reference such methods only from inside your anonymous class itself or through reflection.

This technique is mostly useful when you declare abstract method in your enumeration, and implement that method for each enum individually.

(*) JLS 8.9 Enums part says: "The optional class body of an enum constant implicitly defines an anonymous class declaration (§15.9.5) that extends the immediately enclosing enum type."

like image 27
Peter Štibraný Avatar answered Nov 06 '22 03:11

Peter Štibraný