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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:08:31+00:00 2026-05-23T00:08:31+00:00

I have a set of singleton classes and I want to avoid boilerplate code.

  • 0

I have a set of singleton classes and I want to avoid boilerplate code. Here is what I have now:

public class Mammal {
    protected Mammal() {}
}

public class Cat extends Mammal {
    static protected Cat instance = null;
    static public Cat getInstance() {
        if (null == instance) {
            instance = new Cat();
        }
        return instance;
    }

    private Cat() {
        // something cat-specific
    }
}

This works and there’s nothing wrong with it, except that I have many subclasses of Mammal that must replicate the getInstance() method. I would prefer something like this, if possible:

public class Mammal {
    protected Mammal() {}

    static protected Mammal instance = null;

    static public Mammal getInstance() {
        if (null == instance) {
            instance = new Mammal();
        }
        return instance;
    }
}

public class Cat extends Mammal {
    private Cat() {
        // something cat-specific
    }
}

How do I do that?

  • 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-23T00:08:32+00:00Added an answer on May 23, 2026 at 12:08 am

    You can’t since constructors are neither inherited nor overridable. The new Mammal() in your desired example creates only a Mammal, not one of its subclasses. I suggest you look at the Factory pattern, or go to something like Spring, which is intended for just this situation.

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

Sidebar

Related Questions

I have a set of classes with static methods. Example member of class set:
I have a set of functions I want to be available to my web
I want to create a set of classes that share a lot of common
What if I have few factory methods returning non-public type and pairing set of
I have a class representing a set of values that will be used as
I have a static class which I use to get access to my public
I have a lot of classes in my project accessed by a singleton like
i have simple singleton that sets pdo object from factory class , in the
We have set up a system where notifications get sent to a user with
I have set a canvas' background to an image of a company logo. I

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.