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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:44:27+00:00 2026-05-26T12:44:27+00:00

I am using an enum singleton that looks (somewhat) like this: public enum mySingleton{

  • 0

I am using an enum singleton that looks (somewhat) like this:

public enum mySingleton{

  INSTANCE;

  private static String myDrink = null;

  public String getMyDrink(boolean isWizard)
  {
    if (myDrink == null)
    {
      if (isWizard)
        myDrink = "Butterbeer";
      else
        myDrink = "Whiskey";
    }
    return myDrink;
  }

 //Some more functionality
 //...
}

Now, to test this singleton i have a few tests that use it. But since all the test run one after the other in the same thread, once i run the first test, myDrink is set for all the other tests.

I don’t like that.

I was thinkin of using an @After function and use reflection to do set myDrink to null.

Iv’e tried this:

Field f = mySingleton.class.getField("myDrink");
f.setAccessible(true);
f.set(String.class, null);

But i get a java.lang.NoSuchFieldException.

How can it be done?

  • 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-26T12:44:28+00:00Added an answer on May 26, 2026 at 12:44 pm

    The myDrink field shouldn’t be static. Make it a non-static field, and then use

    f.set(mySingleton.INSTANCE, null);
    

    Also, use getDeclaredField instead of getField. getField looks for public fields.

    If you can’t change it to non-static, then the first argument is ignored, so just use

    f.set(null, null);
    
    • 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 singletom pattern like this: public enum LicenseLoader implements ClientLicense
I'm using an enum singleton, but implementing logging is troublesome. This: public enum Foo
Say you have an enum singleton: public enum Elvis { INSTANCE; private int age;
I have a database table using an enum. This is already working with hibernate
In the external code that I am using there is enum: enum En {VALUE_A,
I want to convert an string to an enum type using TValue, I googled
Is it possible to map an enum as a string using Fluent Nhibernate?
In my project i'm using enums example: public enum NcStepType { Start = 1,
The application retrieve window handles, using Enum* routines. It happens that in the while
I read a lot on stackoverflow regarding the creation of singleton classes using enum.

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.