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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:42:38+00:00 2026-05-28T03:42:38+00:00

The question says it all. I know the Singleton pattern (with final to its

  • 0

The question says it all.
I know the Singleton pattern (with final to its class) is a solution. Are there any other possible ways we can achieve this?
Abstracting a class makes it non-instantiable. Making it final makes it non-inheritable.
How do we combine both?

public final class SingletonObject
{
  private SingletonObject()
  {
    // no code req'd
  }

  /*public static SingletonObject getSingletonObject()
  {
    if (ref == null)
        // it's ok, we can call this constructor
        ref = new SingletonObject();        
    return ref;
  }*/

  public Object clone()
    throws CloneNotSupportedException
  {
    throw new CloneNotSupportedException(); 
    // that'll teach 'em
  }

  private static SingletonObject ref;
}

Code Ref: http://www.javacoffeebreak.com/articles/designpatterns/index.html

  • 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-28T03:42:39+00:00Added an answer on May 28, 2026 at 3:42 am

    Make the constructor private:

    public final class Useless {
        private Useless() {}
    }
    

    A private constructor is the normal object-oriented solution. However, it would still be possible to instantiate such a class using reflection, like this:

    Constructor<Useless> con = Useless.class.getDeclaredConstructor();
    con.setAccessible(true); // bypass "private"
    Useless object = con.newInstance();
    

    To prevent even reflection from working, throw an exception from the constructor:

    public final class Useless {
        private Useless() {
            throw new UnsupportedOperationException();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question says it all: Are there any pitfalls with allocating exceptions on the heap?
Question says it all. Does anyone know if the following... size_t div(size_t value) {
The question says it all basically. I want in a class MyClass to listen
The question says it all. I can't seem to find any recent rails tutorials
The question says it all, really. I know a (Scala) Monad looks like this:
The above question says it all. I know you can create a PDF from
Like the question says, I wanted to know if it's possible to turn off
The question says it all, we know most of the hosted web-based UI prototyping
The question says it all. I want to know if it a good idea
Question says it all, really. My application is a time tracker. It's currently written

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.