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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:39:43+00:00 2026-05-14T21:39:43+00:00

Edit: Answered – error was method wasn’t static I’m used the Singleton Design Pattern

  • 0

Edit: Answered – error was method wasn’t static

I’m used the Singleton Design Pattern

 public class Singleton {
   private static final Singleton INSTANCE = new Singleton();

   // Private constructor prevents instantiation from other classes
   private Singleton() {}

   public static Singleton getInstance() {
      return INSTANCE;
   }
 }

My question is how do I create an object of class Singleton in another class?

I’ve tried:

Singleton singleton = new Singleton(); 
// error - constructor is private
Singleton singleton = Singleton.getInstance();
// error - non-static method cannot be referenced from a static context

What is the correct code?

Thanks,
Spencer

  • 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-14T21:39:44+00:00Added an answer on May 14, 2026 at 9:39 pm
    Singleton singleton = Singleton.getInstance();
    

    is the correct way. Make sure your getInstance() method is indeed static.

    Since your Singleton implementation is far from being safe – your object can be instantiated via reflection, you may want to create a singleton based on enum

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

Sidebar

Related Questions

Edit: Answered my own question. See below. -_- I have a variable defined in
Edit: Answered on serverfault. Thanks! A product I'm testing appears to freak out when
EDIT after answered: < should be provided for std::map . For more information about
EDIT: See below unanswered part II. (ANSWERED) I. Appending URLS I am trying to
EDIT -- Answered below, missed the angled braces. Thanks all. I have been attempting
Let's say that I have two interfaces and a behavior class: public interface Creable
Edit: I think this has been answered successfully, but I can't check 'til later.
Okay the previous question was answered clearly, but i found out another problem. What
EDIT 2 Thank you all for your help! By fusing the answers and some
Just doing a quick edit up at the top of my post. The question

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.