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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:30:00+00:00 2026-05-23T11:30:00+00:00

I am trying to create a Singleton class, which will be accessed from two

  • 0

I am trying to create a Singleton class, which will be accessed from two other classes.Can anyone please tell me whats wrong with the following code? I am just not able to figure out!

import java.util.LinkedList;

public class MessageQueue {

    private static final LinkedList<ServerDataEvent> queue = new LinkedList<ServerDataEvent>();;

    private static MessageQueue messageQueue = null;

    /** A private Constructor prevents any other class from instantiating. */
    private MessageQueue() {
    }

    /** Static 'instance' method */
    public static MessageQueue getInstance() {
        if (MessageQueue.messageQueue == null) {
            System.out.println("Creating MessageQueue instance.");
            MessageQueue.messageQueue = new MessageQueue();
        }
        return MessageQueue.messageQueue;
    }

    public Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }
}

I am accessing the singleton object from other classes like this:

MessageQueue messageQueue = MessageQueue.getInstance();

There are no errors, but

System.out.println("Creating MessageQueue instance.");

is getting executed whenever I do

MessageQueue messageQueue = MessageQueue.getInstance();

EDIT 1

import java.util.LinkedList;

public class MessageQueue {

    private static final LinkedList<ServerDataEvent> queue = new LinkedList<ServerDataEvent>();;

    private static final MessageQueue messageQueue = new MessageQueue();

    /** A private Constructor prevents any other class from instantiating. */
    private MessageQueue() {
        System.out.println("problem...");
    }

    /** Static 'instance' method */
    public static MessageQueue getInstance() {
        return MessageQueue.messageQueue;
    }

    public Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }
}
  • 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-23T11:30:01+00:00Added an answer on May 23, 2026 at 11:30 am

    It is much better to define and instantiate your singleton object like this:

    private static final MessageQueue messageQueue = new MessageQueue();
    

    And then getInstance will be just:

    public static MessageQueue getInstance() {
       return MessageQueue.messageQueue;
    }
    

    This way your singleton object is instantiated and will be thread safe because it is created by the class loader.

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

Sidebar

Related Questions

I'm trying to create a global (singleton) class that can associate any type of
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
i created a singleton class and trying to access that class in other class
I'm trying to figure out how to launch a Service from a singleton class
I am trying to create a singleton User class in my app, here's the
If i create a singleton class then only one object of that class can
How do you create a instance of a singleton that can be used in
OK so im trying to create a settings class to store certain strings that
I have some confusion with singleton class, below are my some points: Can singleton
I'm trying to creating a Singleton class like below where MyRepository lies in separate

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.