Today in my interview one interviewer asked me to write a Singleton class. And i gave my answer as
public class Singleton {
private static Singleton re
Latest Standard Solutions:
Core java with Managed Beans / CDI
@ApplicationScoped
public class MySingleton { ... }
EJB Lite (JEE6)
@Singleton
public class MySingleton { ... }
Prior Recommendation (from 'Effective Java 2'):
Use an enum, with a single bogus enumeration constant e.g. INSTANCE. Data fields and methods can be either static or non-static (instance) - the two behave equivalently, since there's only one instance
Advantages:
Disadvantages (compared with above Standard Solutions):