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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:22:32+00:00 2026-05-11T18:22:32+00:00

Ok, the question might not be crystal clear. Let me give some details: Let’s

  • 0

Ok, the question might not be crystal clear. Let me give some details:

Let’s say I have an Shoe (CShoe) object factory class called CFactory.
CFactory is a singleton class that creates and stores all instanciated shoes using a simple hashmap. It is then accessed through static methods to use the created objects.

Is there a way to force CShoe’s constructor so that it can only called by the factory? (in other words, ensure that the creation of shoes can only be done by the shoes factory singleton class and not by other classes)

  • 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-11T18:22:32+00:00Added an answer on May 11, 2026 at 6:22 pm

    You could make Shoe an inner class of ShoeFactory:

    public class ShoeFactory {
    
        public static class Shoe {
            private String name;
    
            private Shoe() {
            }
    
            private Shoe(String name) {
                this.name = name;
            }
        }
    
        public static Shoe createShoe(String shoeName) {
            return new Shoe(shoeName);
        }
    }
    

    I think this pretty much covers all the cases except …. reflection:

    public class SmellyShoe {
    
        public static void main(String[] args) {
            try {
                java.lang.reflect.Constructor c = Shoe.class.getDeclaredConstructors()[0];
                c.setAccessible(true);
                Shoe smelly = (Shoe)c.newInstance(null);
                // grr
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The question might not be clear, so i will explain further. I saw some
Before answering, it is not as easy question as you might have thought about
This might not be a hard core programming question, but it's related to some
Might not be a very consequential question, but... I have a bunch of mostly-static
My question might not be quite clear... so lets get to samples datas... fileA
New to Web2py, so my question might not be too clear. I'm trying to
I realize that this question might not make sense to some, but I was
It might be for pedantic purposes but not homework. I have below question about
This question might not seem programming related at first, but let me explain. I'm
Technically this might not be classed as a programming question, since I have already

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.