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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:04:10+00:00 2026-05-23T10:04:10+00:00

I have an array of strings containing names of classes. Is it possible to

  • 0

I have an array of strings containing names of classes. Is it possible to invoke the static methods of the actual class using the ‘name of the class’ in the string array.

public class SortCompare {

    // There are classes called 'Insertion', 'Selection' and 'Shell' which have a 
    //   method called 'sort'
    private static String[] algorithm = {  "Insertion", "Selection", "Shell"};

    public static double timeTheRun(String alg, Comparable[] a) {

        for (int i = 0; i < algorithm.length; i++)
            if (alg.equalsIgnoreCase(algorithm[i])) {
                Stopwatch timer = new Stopwatch();

                 // I want to invoke one of Insertion.sort(), Selection.sort()
                 // or Shell.sort() depending on the value of 'alg' here

                 break;
            }
                return timer.elapsedTime();
        }

I could forget about the array of strings and simple use a if-else block to invoke them.

         if (alg.equals("Insertion"))
            Insertion.sort(a);
         else if (alg.equals("Selection"))
            Selection.sort(a);
         else if (alg.equals("Shell"))
            Shell.sort(a);

But I will keep implementing other sorts and variations of them in future and every time I will have to make changes in multiple places(The above if-else loop, the help message of my program). If the former approach is possible then I’ll just have to insert an extra string to the array every time.

  • 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-23T10:04:11+00:00Added an answer on May 23, 2026 at 10:04 am

    The better way to implement this would be to create a common interface for your sorting algorithms:

    interface SortingAlgorithm {
        public void sort(Comparable[] a);
    };
    

    Then have all your algorithms implement that interface:

    class InsertionSort implements SortingAlgorithm {
        public void sort(Comparable[] a) {
            // sort here using insertion-sort
        }
    };
    

    and make the parameter to your method take an implementation of the interface:

    public static double timeTheRun(SortingAlgorithm alg, Comparable[] a) {
        // all the setup
        alg.sort(a);
        // all the post-processing
    }
    

    You would then call that method like this:

    timeTheRun(new InsertionSort(), data);
    

    This has the disadvantage that you cannot make the sorting-routine a static method, though.

    Alternative If you insist on static methods, make your routine take a class-object as parameter:

    public static double timeTheRun(Class algClass, Comparable[] a) {
        // all the setup
        algClass.getMethod("sort", Comparable[].class).invoke(null, a);
        // all the post-processing
    }    
    

    Note that you will either have to add a try-catch-block or a throws declaration for the various exceptions that the reflection methods can throw. Then you can call it like this:

    timeTheRun(InsertSort.class, data);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a single array json string containing data: [{Name:John,Age:22}, {Name:Jack,Age:56}, {Name:John,Age:82}, {Name:Jack,Age:95}] I
I have a 2d array with rows containing an id number, a name string,
I have array of strings, String[] data and it's 10 elements has value P
I have an array of strings plus one additional string. I want to use
I have a class containing Enum with values. (names) In other class I would
I have several string each containing a JSON representation of an array of objects.
I have a 2d array containing first name, last name, and a third irrelevant
I have an array containing names of items. I want to give the user
I have a string containing html and tags in the form [realtor:name] or [office:phone].
I have an array of objects containing pairs of string labels and values, how

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.