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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:13:55+00:00 2026-05-26T15:13:55+00:00

So I have a few ‘Manager’ classes, for example GroupManager. All these Managers are

  • 0

So I have a few ‘Manager’ classes, for example GroupManager. All these Managers are singletons.

Using this method for instancing:

private static GroupManager groupManager = null;

private GroupManager()
{

}

public static GroupManager Instance()
{
    if (groupManager == null)
    {
        groupManager = new GroupManager();
    }
    return groupManager;
}

I’m thinking I should start to use some inheritance as they have a lot of copied methods.

The Instance() methods for each Manager is the same.

So for inheritance i can do this (obviously):

GroupManager extends Manager

Is it possible to use generics to use the same Instance method for all managers, something like:

public class Manager<E>
{
    private static E instance = null;

    public static E Instance()
    {
        if (instance == null)
        {
            instance = new E();
        }
        return instance;
    }

}

I think that makes sense 🙂

So then you would do GroupManager.Instance() like normal.

  • 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-26T15:13:56+00:00Added an answer on May 26, 2026 at 3:13 pm

    You don’t understand how generics and statics work. If you have a static field or method (such as “instance” or instance()), which can be called without instantiating the class Manager, how do you expect the JVM (and the compiler even) to know what type E is supposed to be?

    Here’s an example, as per G_H’s suggestion:

    GeneralManager and AreaManager both extend Manager

    The Manager class is the only one that has the getInstance() static method:

        public class Manager {
    
            private static Map<Class<? extends Manager>,Manager> INSTANCES_MAP = new java.util.HashMap<Class<? extends Manager>, Manager>();
    
    //Also, you will want to make this method synchronized if your application is multithreaded,
    //otherwise you mihgt have a race condition in which multiple threads will trick it into
    //creating multiple instances
            public static <E extends Manager> E getInstance(Class<E> instanceClass) throws InstantiationException, IllegalAccessException {
                if(INSTANCES_MAP.containsKey(instanceClass)) {
                    return (E) INSTANCES_MAP.get(instanceClass);
                } else {
                    E instance = instanceClass.newInstance();
                    INSTANCES_MAP.put(instanceClass, instance);
                    return instance;
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have few question in this regard When you create an internet page, does
In almost all of my projects I have few files that are project wide
I have few databses of sql 2000 on windows 2000. Can I attach these
I have few nested DIV elements. For example: <div id='id1'><div id='id2'>content</div></div> I attach event
i have few questions for SQL gurus in here ... Briefly this is ads
I have few simple django-based sites and I their number increasing all the time.
I have few arrays of ccspirit images, these are balloons of different colors, like
I have few tabs with input on each tab. I am using and I
I have few questions on this as selenium always need windowid to get control
I have few questions about this code: <?php $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); $iv =

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.