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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:28:28+00:00 2026-05-27T18:28:28+00:00

I need to create a singleton class without keeping a static method. How can

  • 0

I need to create a singleton class without keeping a static method.

How can i do that?

  • 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-27T18:28:29+00:00Added an answer on May 27, 2026 at 6:28 pm

    Create an enum with one instance

    enum Singleton {
        INSTANCE;
    
        private Field field = VALUE;
        public Value method(Arg arg) { /* some code */ }
    }
    
    // You can use
    Value v = Singleton.INSTANCE.method(arg);
    

    EDIT: The Java Enum Tutorial shows you how to add fields and methods to an enum.


    BTW: Often when you can use a Singleton, you don’t really need one as utility class will do the same thing. The even shorter version is just

    enum Utility {;
        private static Field field = VALUE;
        public static Value method(Arg arg) { /* some code */ }
    }
    
    // You can use
    Value v = Utility.method(arg);
    

    Where Singletons are useful is when they implement an interface. This is especially useful for testing when you using Dependency injection. (One of the weakness of using a Singleton or utility class substitution in unit tests)

    e.g.

    interface TimeService {
        public long currentTimeMS();
    }
    
    // used when running the program in production.
    enum VanillaTimeService implements TimeService {
        INSTANCE;
        public long currentTimeMS() { return System.currentTimeMS(); }
    }
    
    // used in testing.
    class FixedTimeService implements TimeService {
        private long currentTimeMS = 0;
        public void currentTimeMS(long currentTimeMS) { this.currentTimeMS = currentTimeMS; }
        public long currentTimeMS() { return currentTimeMS; }
    }
    

    As you can see, if your code uses TimeService everywhere, you can inject either the VanillaTimeService.INSTANCE or a new FixedTimeService() where you can control the time externally i.e. your time stamps will be the same every time you run the test.

    In short, if you don’t need your singleton to implement an interface, all you might need is a utility class.

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

Sidebar

Related Questions

I have a method in a singleton class that need to use the .NET
Ok, here is my problem. I need to create a socket program that can
I have a singleton class that is shared by some threads. within a method
I need create clone repository. but I do not know where can I get
I need create a document word with Java. And I ask, how can I
i need create a variable with parent subclass. Example: Parent Class <?php class parentClass
I need to create an XML schema that looks something like this: <xs:element name=wrapperElement>
I'm working on creating a Hibernate query engine that can create dynamic queries. Thus
So I have this method that get executed repeatedly public static boolean isReady(String dirPath,
Possible Duplicate: Difference between static class and singleton pattern? we use static class for

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.