(JAVA Enums) - Anonymous class inside enum constant

Dub_

Good day!

I have an interface which only implements one single method. I dont feel like making several class which all implement this one single method therefore I decided to use anonymous classes instead.

I use enums for certain static items, these enums have instances of my interface. However, when I try to make an anonymous class inside my enum constants my IDE (eclipse) literally tells me nothing (as if it is outside a code block).

My question is as follows: Can I use anonymous classes inside my enum constants?

If my text was unclear (Sorry im not english) please see the example below.

Code example

/**
 * My Interface 
 */

public interface IPotato {

    public void eatPotato();
} 

/**
* My enum class
*/
    public enum PotatoEnum {

        I_WANT_TO_EAT_POTATO(new IPotato() {
            @Override
            public void eatPotato() {
                // Cant put code here.
            } });

        private IPotato _myAnonymousClass;
        private PotatoEnum(IPotato anonymousClass){
            this._myAnonymousClass = anonymousClass;
        }

        public IPotato getPotato(){
            return _myAnonymousClass;
        }

    }
user902383

You could do that, it is a perfectly valid solution.

As a recommendation, make your enum implement your interface to make the code more readable:

public enum PotatoEnum implements IPotato{

        I_WANT_TO_EAT_POTATO(){

            @Override
            public void eatPotato() {
                // Cant put code here.

            }},//more ENUMS ;

    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java enum anonymous inner class and reflection

From Dev

Java: Defining a generic method inside an anonymous class

From Dev

Compile error in swift when accessing enum inside a Constant class

From Dev

Private enum location inside a class in Java

From Dev

clojure access enum defined inside a java class

From Dev

Java Enum group enums by their values

From Dev

Constant inside class

From Dev

Constant inside class

From Dev

Java return method from inside anonymous inner class

From Dev

What is the best way to store a reference to an arbitrary class inside an enum constant using the least memory on initialization as possible?

From Dev

Enum Inside Class

From Dev

Java Enums: Instantiate Enum "dynamically" (more like a class constructor) to use with Jackson

From Dev

Java Enums: Instantiate Enum "dynamically" (more like a class constructor) to use with Jackson

From Dev

Mocking with anonymous class in Java

From Dev

An enum constant contains all the enum constants of the same enum class

From Dev

What's the benefit of using enum embedded inside a class in JAVA?

From Dev

What's the benefit of using enum embedded inside a class in JAVA?

From Dev

Define static anonymous enum outside the class

From Dev

Define static anonymous enum outside the class

From Dev

Is there a way to access an anonymous class inside another anonymous class?

From Dev

Refactor Constant Class to Enum in Android Studio

From Dev

enum's constant value adding onymous class

From Dev

Is creating a constant or enum class for template page efficient?

From Dev

Java final class with constant

From Dev

Replace enum with struct inside a class

From Dev

How to stop Swing timer inside the anonymous class?

From Dev

Accessing variables from inside anonymous class

From Dev

How to stop Swing timer inside the anonymous class?

From Dev

What calls a method inside an anonymous inner class?

Related Related

  1. 1

    Java enum anonymous inner class and reflection

  2. 2

    Java: Defining a generic method inside an anonymous class

  3. 3

    Compile error in swift when accessing enum inside a Constant class

  4. 4

    Private enum location inside a class in Java

  5. 5

    clojure access enum defined inside a java class

  6. 6

    Java Enum group enums by their values

  7. 7

    Constant inside class

  8. 8

    Constant inside class

  9. 9

    Java return method from inside anonymous inner class

  10. 10

    What is the best way to store a reference to an arbitrary class inside an enum constant using the least memory on initialization as possible?

  11. 11

    Enum Inside Class

  12. 12

    Java Enums: Instantiate Enum "dynamically" (more like a class constructor) to use with Jackson

  13. 13

    Java Enums: Instantiate Enum "dynamically" (more like a class constructor) to use with Jackson

  14. 14

    Mocking with anonymous class in Java

  15. 15

    An enum constant contains all the enum constants of the same enum class

  16. 16

    What's the benefit of using enum embedded inside a class in JAVA?

  17. 17

    What's the benefit of using enum embedded inside a class in JAVA?

  18. 18

    Define static anonymous enum outside the class

  19. 19

    Define static anonymous enum outside the class

  20. 20

    Is there a way to access an anonymous class inside another anonymous class?

  21. 21

    Refactor Constant Class to Enum in Android Studio

  22. 22

    enum's constant value adding onymous class

  23. 23

    Is creating a constant or enum class for template page efficient?

  24. 24

    Java final class with constant

  25. 25

    Replace enum with struct inside a class

  26. 26

    How to stop Swing timer inside the anonymous class?

  27. 27

    Accessing variables from inside anonymous class

  28. 28

    How to stop Swing timer inside the anonymous class?

  29. 29

    What calls a method inside an anonymous inner class?

HotTag

Archive