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 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

Related Questions

I have this kind of structure that I want to both read and write
In short, if you want to write a map of e.g. constants in Java,
Using GWT I have a Java class: public class Pojo { private String name;
Consider the following class written in Java: class NonNegativeDouble { private final double value;
Initially i thought to do something like: #EXIT CODES class ExitCode(object): (USERHOME_INVALID, \ USERHOME_CANNOT_WRITE,
I am trying to understand how the following code ties into two other .java
I am looking for a way to transform some classes from Java to .Net
I am new to eclipse + Java. I am trying to create executable jar
I have a whole bunch of unit tests written in MbUnit and I would
Before anything, let me first clarify that the below thoughts are purely my personal

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.