daily pastebin goal
29%
SHARE
TWEET

Untitled

a guest Feb 19th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Revolver {
  2.  
  3.     private static Revolver[] instance = new Revolver[6];
  4.     private static int nextInstanceNumber = 0;
  5.  
  6.     private Revolver() {
  7.     }
  8.  
  9.     public static Revolver nextInstance() {
  10.         if (nextInstanceNumber == 6) {
  11.             nextInstanceNumber = 0;
  12.         }
  13.         if (instance[nextInstanceNumber] == null){
  14.             System.out.println("Creating new instance");
  15.             instance[nextInstanceNumber] = new Revolver();
  16.         }
  17.         return instance[nextInstanceNumber++];
  18.     }
  19.  
  20.     public static int getNextInstanceNumber(){
  21.         return nextInstanceNumber;
  22.     }
  23. }
  24.  
  25. public class Main {
  26.  
  27.     public static void main(String[] args){
  28.         Revolver revolver = Revolver.nextInstance();
  29.         System.out.println(revolver.getNextInstanceNumber() - 1);
  30.         for (int i=0; i<15; i++){
  31.             revolver = Revolver.nextInstance();
  32.             System.out.println(revolver.getNextInstanceNumber() - 1);
  33.         }
  34.     }
  35.  
  36. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top