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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:41:15+00:00 2026-05-16T03:41:15+00:00

I want to write a java class that can be instantiated only 5 times

  • 0

I want to write a java class that can be instantiated only 5 times just like you have singleton class which have only one instance.

Apart from this the instances should be picked in round robin basis.

Suppose I have a class A. I should be only able to create 5 instances of this class.
Say I have InstanceA_1, InstanceA_2, InstanceA_3, InstanceA_4, InstanceA_5. Whenever I need to use them they should be picked on round robin basis.

  • 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-16T03:41:15+00:00Added an answer on May 16, 2026 at 3:41 am

    Just as enum is recommended by Effective Java 2nd Edition to implement singleton, this solution also uses enum to implement… quadrupleton?

    import java.util.*;
    
    public enum RoundRobin {
        EENIE, MEENIE, MINY, MO;
    
        private final static List<RoundRobin> values =
            Collections.unmodifiableList(Arrays.asList(values()));
        // cache it instead of creating new array every time with values()
    
        private final static int N = values.size();
        private static int counter = -1;
    
        public static RoundRobin nextInstance() {
            counter = (counter + 1) % N; // % is the remainder operator
            return values.get(counter);
        }
    
        public static void main(String[] args) {
            for (int i = 0; i < 10; i++) {
                System.out.println(RoundRobin.nextInstance());
            }
            // EENIE, MEENIE, MINY, MO, EENIE, MEENIE, MINY, MO, ...
        }
    }
    

    Extending this to quintupleton is self-explanatory.

    See also

    • Effective Java 2nd Edition, Enforce singleton property with a private constructor or an enum type

      As of release 1.5., there is a third approach to implementing singletons. Simply make an enum type with one element. This approach is functionally equivalent to the public field approach, except that it is more concise, provides serialization mechanism for free, and provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks. While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a singleton.

    Related questions

    • Efficient way to implement singleton pattern in Java
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 497k
  • Answers 497k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Solved by modifying the [CaptchaValidatorAttribute] OnActionExecuting method, where CaptchaSuccessFieldKey refers… May 16, 2026 at 12:05 pm
  • Editorial Team
    Editorial Team added an answer I can't replicate the problem -- using .NET 3.5 (32-bit)… May 16, 2026 at 12:05 pm
  • Editorial Team
    Editorial Team added an answer The OP's "settled on" solution has serious bugs (self.date needs… May 16, 2026 at 12:05 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I need to write a Java Comparator class that compares Strings, however with one
I have a Java program that loads thirdparty class files (classes I did not
I have a Java application and I would like to make it extensible. To
Hello I want write my own desktop sharing application in Java. The application should
I want to use the Java API in my NetLogo program. For that, I
I want to write network address to my listview, in a range like 192.168.0.0
I want to write a vbscript that will wait for windows shutdown and intercept
I have defined the following Unit-Test: [TestMethod] //@Test for the Java crowd public void
I'm using Netbeans to write Scala and Java. Netbeans generated a .jar file for
I have installed the NetBeans IDE. Now I want to start to do something.

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.