o
A singleton class has
only single instance
o
A single object can be used by all other classes.
o
It is used in multi-threaded and database
applications, configuration settings etc.
o Singleton class should have static member ,
private constructor and static method
public class SingleTon1{
private
static SingleTon1 obj; //Static member
private SingleTon1(){ //Private Constructor
System.out.println("Hi User");
}
public
static SingleTon1 getInstance(){
//Static method
if(obj == null){
obj =
new SingleTon1();
}
return
obj;
}
public
void Display(){
System.out.println("Welcome to
LearingPoint92");
}
public
static void main(String a[]){
SingleTon1 st =SingleTon1.getInstance()
st.Display();
}
}
No comments:
Post a Comment