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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:51:20+00:00 2026-05-22T20:51:20+00:00

I want to reference an enum method to retrieve the class of an algorithm

  • 0

I want to reference an enum method to retrieve the class of an algorithm so that I can lazy-load a new instance of the algorithm for use in the strategy design pattern.

In this example, I am using an enum to reference three different strategy classes that calculate Fibonacci numbers: RecursiveFibonacciGenerator, IterativeFibonacciGenerator, and MemoizedFibonacciGenerator (all of which inherit from FibonacciGenerator).

The code (with lines generating errors commented with intent) is as follows:

package com.example.strategy;

public class Fibonacci {
    private enum Algorithm {
        RECURSIVE (RecursiveFibonacciGenerator.class),
        ITERATIVE (IterativeFibonacciGenerator.class),
        MEMOIZED (MemoizedFibonacciGenerator.class);

        private final Class<T> algorithmClass; // Declare class of same type as constructor
        private final T instance; // Declare instance of class defined in constructor
        private <T extends FibonacciGenerator> Algorithm(Class<T> algorithmClass) {
            this.algorithmClass = algorithmClass;
        }

        public T getInstance() {
            if (this.instance == null) {
                this.instance = this.algorithmClass.newInstance();
            }
            return this.instance;
        }
    }

    public Integer getTerm(Integer termNumber) {
        profileGenerator(termNumber, Algorithm.RECURSIVE);
        profileGenerator(termNumber, Algorithm.ITERATIVE);
        return profileGenerator(termNumber, Algorithm.MEMOIZED);
    }

    private Integer profileGenerator(Integer termNumber, Algorithm algorithm) {
        System.out.print("Computing term using " + algorithm.toString() + " algorithm... ");
        Long startTimeMilliseconds = System.currentTimeMillis();
        Integer term = algorithm.getInstance().generateTerm(termNumber);
        Long endTimeMilliseconds = System.currentTimeMillis();
        Long computationTimeMilliseconds = endTimeMilliseconds - startTimeMilliseconds;
        System.out.println("term computed in " + computationTimeMilliseconds + " milliseconds");
    }
}

I would like to know how I can use this enum constructor to store a member variable of the Class<T> type.

Edit: Added full code to clarify intent

  • 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-22T20:51:21+00:00Added an answer on May 22, 2026 at 8:51 pm
    public enum Algorithm {
        RECURSIVE(FibonacciGenerator.RecursiveFibonacciGenerator.class),
        ITERATIVE(FibonacciGenerator.IterativeFibonacciGenerator.class),
        MEMOIZED(FibonacciGenerator.MemoizedFibonacciGenerator.class);
    
        private final Class<? extends FibonacciGenerator> algorithmClass;
    
        private <T extends FibonacciGenerator> Algorithm(Class<T> algorithmClass) {
            this.algorithmClass = algorithmClass;
        }
    }
    

    Does that do what you want?

    The other option is to use an instance of the class, but after further thought I think this is a bad idea. If you are going to use an instance of the class then why’d you need the enum in the first place?

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

Sidebar

Related Questions

well i want an enum i can use in switch function. i need to
In this particular code sample I want to reference the second overloaded method (int
I want to know how we can get record count using reference cursor in
I want to reference a alue in a header file to use in my
Can I use a marker annotation on an enum when designing a generic interface?
I want to implement an extension method which converts an enum to a dictionary:
Curious situation: public class MyTextBox : TextBox { // I want use the same
I have CSV files that contain numerous values that I want to reference. I
I want to reference a COM DLL in a .NET project, but I also
I am using the pimpl idiom and want to reference one of the methods

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.