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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:30:00+00:00 2026-06-04T00:30:00+00:00

If you have a Java Singleton that looks like this: public class MySingleton {

  • 0

If you have a Java Singleton that looks like this:

public class MySingleton {

private static MySingleton instance;

private int member;

public static MySingleton getInstance(){
    if(instance==null){
        instance = new MySingleton();
    }
    return instance;
}

private MySingleton(){
    //empty private constructor
}

public int getMemberA(){
    return member;
}

public int getMemberB(){
    return instance.member;
}

}

…is there a difference between getMemberA and getMemberB? That is, is there a difference between accessing the member with instance.xxx and just xxx?

Note: I am aware of the pros and cons of using the Singleton pattern!

  • 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-06-04T00:30:01+00:00Added an answer on June 4, 2026 at 12:30 am

    Yes, there’s a difference.

    Your singleton implementation isn’t currently threadsafe, which means it’s possible to call getMemberB() on an instance other than the one referred to by instance, at which point you’ll get a different result.

    If your implementation were thread-safe (so genuinely only one instance could ever be created) then they’d be equivalent, and the simpler form would be much preferred.

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

Sidebar

Related Questions

I am using an enum singleton that looks (somewhat) like this: public enum mySingleton{
I have a java singleton class that has my application settings. I used this
I have a class MyClass that has a static variable. Class implements smth like
I have a normal java bean which is singleton with jms resource like this
I come from java/cpp environment where singletons do have their advantages over class(static) methods,
I have a singleton class, that has a map which can be accessed by
Simple question, I have code like this: class Context[A] { def t: A }
I have a class with a collection of Wildcard Types that is a singleton,
I have a static class with a static constructor that takes some time (10-15
I have Java Map (out of Strings and Ints) objects that I want to

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.