I’m implementing a deck of cards as a singleton, using an enum so that I can have universal access to the deck from anywhere in the program. As part of the enum’s constructor, it shuffles an arraylist with every card and pushes them all onto a stack (it just seems symbolically appropriate).
When is that constructor called? Is it constructed at runtime? Compile time? The first time I make a call to the enum? Can I expect that every time I run the program that the deck of cards will be properly randomized?
Thanks!
You shouldn’t use a singleton for this.
But to answer the question, the constructor is called when the enum class (Deck) is loaded by the classloader and initialized. So the deck will be shuffled before any line of code can call any method of the Deck class.