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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:20:58+00:00 2026-05-28T05:20:58+00:00

This may be a stupid question but one that is throwing me for a

  • 0

This may be a stupid question but one that is throwing me for a loop.

I have implemented a singleton class (I know they are evil) using an enum in Java as follows

public enum Edit {

   INSTANCE;

   private TreeSet<String> list1 = new TreeSet<String>();

   public void createList(Scanner input) {
        while (input.hasNext()) {
            list1.add(input.next());
        }

My question is what happens with the TreeSet when you use the createList method in more than one place in the application?

For example say the first time Edit.INSTANCE.createList() is called in the application,10 Strings are added to list1, now the second time it is called, 5 Strings are added to list1….will those new 5 strings be added to the TreeSet with the previous 10 Strings or will they be added to a completely different (new) TreeSet?

I know the purpose of a singleton class is to make sure that there is one and only one global instance of the class but does that hold true for the class member variables? Is there only one copy of list1 in the above example or each time the createList() method is called is a new TreeSet created?

  • 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-28T05:20:59+00:00Added an answer on May 28, 2026 at 5:20 am

    There’s just a single instance of the Edit type, so there’s a single TreeSet<String>.

    Each time you call createList, it will add more values to the same TreeSet.

    You say:

    I know the purpose of a singleton class is to make sure that there is one and only one global instance of the class but does that hold true for the class member variables?

    If it didn’t hold for member variables, what would be the point? That’s what’s interesting about a single instance – it has its own state.

    In general, it’s a very bad idea for an enum to be mutable – usually they’re meant to represent specific values, potentially with behaviour. Likewise I rarely have mutable singletons – it introduces threading issues etc, as you naturally have shared state.

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

Sidebar

Related Questions

This may be a stupid question, but I have to ask! I have the
This may be a stupid question but how does one actually run the 'Build'
this may seem like a stupid question but I have a query string which
This may sound like a stupid question, but if one locks a resource in
This question may seem stupid, but, so be it! I have a stupid problem
I know this may be a stupid question to ask but I want to
I'm new to Ruby so this may be a stupid question. I know that,
This may be a stupid question but how does the sizeof operator know the
This may be a stupid question, but it's something that bugs me on a
This may be a stupid question but im just starting to learn Rail thats

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.