I am implementing a singleton class in Java to make sure no more than one instance of the class is created.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
Perhaps the best way is to use an enum with a single instance. This has the added benefit of being serializable and guaranteeing singleton-ness against serialization and reflection, which no “straightforward” Singleton implementation does (private? I have reflection, I scoff derisively at your access modifiers!). It’s also very simple to implement: