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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:36:23+00:00 2026-05-17T21:36:23+00:00

This question, like my previous question, references Effective Java . This time I have

  • 0

This question, like my previous question, references Effective Java. This time I have quite a few sub-questions.

  1. A privileged client can invoke the private constructor reflectively with the aid of the AccessibleObject.setAccessible() method. If you need to defend against this, modify the constructor.
    How, exactly, can a private constructor be invoked? And what is AccessibleObject.setAccessible()?

  2. What approach do you experts follow with singletons?

    // Approach A
    public class Test{
        public static final Test TestInstance = new Test();
        private Test(){ ... }
        .
        .
        .
    }
    
    
    // Approach B
    public class Test{
        private static final Test TestInstance = new Test();
        private Test(){ ... }
        public static Test getInstance() { return TestInstance; }
        .
        .
        .
    }
    

    Isn’t the second approach more flexible, in case we have to make a check for new instances every time or the same instance every time?

  3. What if I try to clone the class/object?

  4. a single-element enum type is the best way to implement a singleton.
    Why? How?

  • 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-17T21:36:24+00:00Added an answer on May 17, 2026 at 9:36 pm

    A priviledged cleint can invoke the private constructor reflectively with the aid of the AccessibleObject.setAccessible method, If you need to defend this, modify the constructor. My question is: How exactly can a private constructor is invoked? and what is AccessibleObject.setAccessible??

    Obviously a private constructor can be invoked by the class itself (e.g. from a static factory method). Reflectively, what Bloch is talking about is this:

    import java.lang.reflect.Constructor;
    
    public class PrivateInvoker {
        public static void main(String[] args) throws Exception{
            //compile error 
    //      Private p = new Private();
    
            //works fine
            Constructor<?> con = Private.class.getDeclaredConstructors()[0];
            con.setAccessible(true);
            Private p = (Private) con.newInstance();
        } 
    }
    
    class Private {
        private Private() {
            System.out.println("Hello!");
        } 
    }
    

    2.What approach do you experts follow with singletons:

    …

    Typically, the first is favoured. The second (assuming you were to test if TestInstance is null before returning a new instance) gains lazy-loading at the cost of needing to be synchronized or being thread-unsafe.

    I wrote the above when your second example didn’t assign the instance to TestInstance at declaration. As stated now, the above consideration is irrelevant.

    Isn’t the second approach more flexible in case we have to make a check for new instance every time or same instance every time?

    It’s not about flexibility, it’s about when the cost of creating the one (and only) instance is incurred. If you do option a) it’s incurred at class loading time. That’s typically fine since the class is only loaded once it’s needed anyway.

    I wrote the above when your second example didn’t assign the instance to TestInstance at declaration. As stated now, in both cases the Singleton will be created at class load.

    What if I try to clone the class/object?

    A singleton should not allow cloning for obvious reasons. A CloneNotSupportedException should be thrown, and will be automatically unless you for some reason implement Cloneable.

    a single-element enum type is the best way to implement a singleton. Why? and How?

    Examples for this are in the book, as are justifications. What part didn’t you understand?

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

Sidebar

Related Questions

This question is a follow up to my previous Java GC question: Java Garbage
This question follows on from a previous question. However stackoverflow presents me from commenting
This question is a follow up of my previous question stack growth direction I
I know this question must have been covered endless of times, but I've searched
Note: This is a follow up to this question . I have a legacy
My question does not really have much to do with sqlalchemy but rather with
This is a noobie question, but I'm not sure how to pass by reference
In this question about including all the classes in a directory for an interpreter
A question of particular interest about python for loops. Engineering programs often require values
I've never asked a question here before so please forgive my question if its

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.