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

  • SEARCH
  • Home
  • 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 7068183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:17:34+00:00 2026-05-28T05:17:34+00:00

I have a clas called as MyFunctions that defines different functions func1 , func2

  • 0

I have a clas called as MyFunctions that defines different functions func1, func2, etc. Also I have a class Process that stores the function name assigned to the object of this class:

Process p1 = new Process();
String fName1 = "func1";
p1.setFunctionName(fName1); 
Process p2 = new Process();
String fName2 = "func2";
p2.setFunctionName(fName2); 

In order to run a proper function, I do the following:

MyFunctions f = new MyFunctions();
if (p.getFunctionName() == "func1") {
   output = f.func1(inputdata);
} else if (p.getFunctionName() == "func2") {
   output = f.func2(inputdata);
}

I´m not sure that this approach is efficient. Is there any other way to solve this task?

Another question: is it possible to do something like this in JAVA?:

String fName = p.getFunctionName();
output = f."+fName+"(input);
  • 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-28T05:17:35+00:00Added an answer on May 28, 2026 at 5:17 am

    First, this approach is not exactly correct. Never use == to compare objects. Use equals() instead:

    MyFunctions f = new MyFunctions();
    if ("func1".equals(p.getFunctionName())) {
       output = f.func1(inputdata);
    } else if ("func2".equals(p.getFunctionName())) {
       output = f.func2(inputdata);
    }
    

    Second, I’d suggest you to use enum instead.

    public enum FunctionInvoker {
        func1 {
            public Object invoke(MyFunctions o, Object ... arg) {
                 o.func1(arg[0]);
            }
        },
        func2 {
            public Object invoke(MyFunctions o, Object ... arg) {
                 o.func2(arg[0], arg[1]);
            }
        },
    }
    

    Now the usage looks like:

    String functionName = ...; // probably read from file, etc.
    Object result = FunctionInvoker.valueOf(functionName).invoke(args);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class LinkedList clas that has a class called Node class Node
I have a skin that's base clas is GraphicRectangularBorderSkin (from degrafa). I'm using it
I have this code. A base class that create a new instance of the
I have a program that creates a C# class from a text file. The
I have 2 questions: suppose we have one entity named class and another called
i have a DetailViewController and a Messages class(this is a TableViewController class). I parse
In my application I have an extended User model called UserProfile. This user can
I need to patch a class that is in a JA file, in a
I am trying to create a a class that will store a time series
Here's my dilemma. I have 2 animations that need to run sequentially. The first

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.