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 6088699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:00:55+00:00 2026-05-23T12:00:55+00:00

I have a class that contains an Enum to do calculation. Each Enum uses

  • 0

I have a class that contains an Enum to do calculation.
Each Enum uses some or all of the non-static variables from the outer class. However since they can’t access instance variable, I have to pass in them as parameters.

public class Outer{
    ClassA a;
    ClassB b;
    ClassC c;
    Map<MyEnum,Double> results= new EnumMap(MyEnum.class);
    private enum MyEnum{
        X{  public double calc(ClassA _a){ dostuff }           },
        Y{  public double calc(ClassB _b,ClassC _c){ dostuff } },
        Z{  public double calc(ClassA _a,ClassB _b){ dostuff } };
    }

    public void doCalc(){
        for(MyEnum item:MyEnum.values()){
            result.get(item) = item.calc(...);//Not uniform here
        }
    }
}

My problem is that I can’t have a uniform way to pass in the parameters in the for-loop. I could make each Enum method takes all classes like

public double calc(ClassA _a,ClassB _b,ClassC _c){ dostuff}

But if I have more classes, the parameter will look too ugly. Is there a better way to do this kind of thing?

  • 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-05-23T12:00:56+00:00Added an answer on May 23, 2026 at 12:00 pm

    Why don’t you pass the instance of outer to the calc() method. In that case, each specific enum will have the logic to deal with the Outer object accordingly and new enums wouldn’t require any change.

    class Outer {
    
        ClassA a;
        ClassB b;
        ClassC c;
        Map<MyEnum,Double> results= new EnumMap<MyEnum, Double>(MyEnum.class);
        private enum MyEnum{
            X{  public void calc(Outer o){ }           },
            Y{  public void calc(Outer o){  } },
            Z{  public void calc(Outer o){ } };
            abstract void calc(Outer o);
        }
    
        public void doCalc(){
            for(MyEnum item:MyEnum.values()){
                item.calc(this);
            }
        }
    }
    
    
    class ClassA {}
    class ClassB {}
    class ClassC {}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class file that contains all the classes that are needed for
I have a program with a class that contains a public enum, as follows:
I have a Class named Constants that contains all the constant variable in my
Basically I have a custom List class that contains different fruits. Assume that each
Good morning SO, I have a class that contains the following static array: public
I have a parent class that contains an array of objects each with a
I have a class that contains a dynamically allocated array, say class A {
I have a class that contains two methods like these: public String getFoo(Int32 a)
I have a class that contains a list of objects. What's the best way
I have a class that contains an array. I want this array to be

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.