Home CPSC 240

Lab 9: Singletons



To get experience using the Singleton pattern.



For this exercise, you will turn a regular class into a singleton class that only allows one instance to be created.

This program contains a class called "Deck" for representing the set of 52 cards. It's possible that we want the same deck of cards to be available to all parts of our program, and that we only want one deck to be created.

For example, if we are writing a Blackjack game, there should only ever be one deck of cards. We would not want to accidentally create two decks and cause there to be duplicates of cards floating around. By making the class into a singleton, we make this impossible.



  1. Download the CardProgram.java file.
  2. Change the Deck class into a singleton using the method discussed in the reading.
  3. Change the main method to get the Deck instance rather than creating a new object.
  4. The program should simply print all 52 cards in random order.



When you are done, please email your .java file to ifinlay@umw.edu.

Copyright © 2019 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.