S

 

Singleton

A singleton is a class for which only one instance can exist within a program. It enforces at most one instance of some class and controlled access to it.

Implementation of a singleton class is as follows:

public class SingletonClass {
  private static final SingletonClass instance = new SingletonClass();

private SingletonClass(){} //Default constructor is private

public static SingletonClass getInstance(){

return instance;
}
}// End of class


The Java spec guarantees that the static initializer will be executed only once, at class load time.

OR

public class SingletonClass {
  /**
* The constructor could be made private
* to prevent others from instantiating this class.
* But this would also make it impossible to
* create instances of Singleton subclasses.
*/
protected SingletonClass(){}

static private instance = new SingletonClass();

public static SingletonClass getInstance(){

return instance;

}
}// End of class




For further reference refer Singleton Pattern in Java Design Patterns.



[ Received from Seeny Desai ]

S
Servlet
Servlet Container
Servlet Context
Servlet Mapping
Session
Session Bean
Singleton
Stateful Session Bean
Stateless Session Bean
Static
Static Field
Static Method
Synchronized
Use of this website constitutes acceptance of the AllAppLabs.com Terms and Conditions