Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7725483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:56:55+00:00 2026-06-01T04:56:55+00:00

Consider the simple example below of implementing a method in an Enum. One problem

  • 0

Consider the simple example below of implementing a method in an Enum. One problem with this method is that, when you have a lot of enum instances, you visually can no longer see them all at once, as a list. That is, if we had many toys, I would like to see “DOLL, SOLDIER, TEDDYBEAR, TRAIN, ETC”, together, in one long list, and then after that list I could implement any needed methods, e.g. methods that are abstract in the enum itself.

Is there any way to do this? Or do you have to implement the methods when you declare the individual enum instances, as in the example below?

public enum Toy {
     DOLL() {
          @Override public void execute() { 
               System.out.println("I'm a doll."); 
          }
     },
     SOLDIER() {
          @Override public void execute() { 
               System.out.println("I'm a soldier."); 
          }
     };
     //abstract method
     public abstract void execute();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:56:57+00:00Added an answer on June 1, 2026 at 4:56 am

    One way that comes to mind is to leave the implementation of the abstract methods to separate implementation classes, something like:

    interface ToyBehaviour {
    
        void execute();
    }
    
    public enum Toy {
         DOLL(new DollBehaviour()),
         SOLDIER(new SoldierBehaviour());
    
         private final ToyBehaviour behaviour;    
    
         Toy(ToyBehaviour impl) {
    
            behaviour = impl;
         }
    
         public void execute() {
    
            behaviour.execute();
         }
    }
    
    class DollBehaviour implements ToyBehaviour {
    
        public void execute() { 
            System.out.println("I'm a doll."); 
        }
    }
    

    This setup would allow you to create behaviour classes in separate files in the case that your implementation has enough complexity to warrant separation.

    In the case that the implementation is simple enough to include it into the one enum class, you can put the interface and behaviour classes as children of the enum class:

    public enum Toy {
         // enum values
         DOLL(new DollBehaviour()),
         SOLDIER(new SoldierBehaviour());
    
         private final ToyBehaviour behaviour;    
    
         Toy(ToyBehaviour impl) {
    
            behaviour = impl;
         }
    
         public void execute() {
    
            behaviour.execute();
         }
    
        // behaviour interface
        interface ToyBehaviour {
    
            void execute();
        }
    
        // behaviour implementation (sub)classes
        static class DollBehaviour implements ToyBehaviour {
    
            public void execute() { 
                System.out.println("I'm a doll."); 
            }
        }
    
        // etc ...
    }
    

    I would probably opt for the first implementation myself, unless the hierarchy of implementation classes is very trivial.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this simple example code: <form name=text id=frm1 method=post> <input type=checkbox name=name[] value=1000> Chk1<br>
I have this sample code ( below ), example1() method works with no problem,
Consider this simple example (which displays in red): echo -e \033[31mHello World\033[0m It displays
I'm designing an HTML page for display in Android browsers. Consider this simple example
Consider this simple example. Class A { B b; A() { this.b = new
Consider this simple example: template <class Type> class smartref { public: smartref() : data(new
Consider this simple xml element example: <parent foo=1 bar=2 foobar=3> <child/> </parent> In the
Consider this simple example; <?php $text = test; ?> <script type=text/javascript defer=defer> var test;
I have a multi-dimensional array similar to the example below that I want to
Consider the following simple example class, which has a property that exposes a modified

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.