I am making a text adventure engine in Java, and in order to save memory, I have a database object that holds all the currently scraped items from an XML document. I want to use it from several different classes. How can I make it available to my classes? Currently I'm using a null static field with an appropriate mutator method.
It might be useful to use a Singleton for this.
You can use a enum type. Joshua Bloch says in his book Efective Java: a single-element enum type is the best way to implement a singleton.
public enum Elvis {
INSTANCE;
public void leaveTheBuilding() { ... }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With