Singleton Class-Private Constructor
A class is called a singleton class, if it allows only one instance of the object to be created throughout the application.
A singleton class has the following
package singleotons;
Public class Logger{
private static Logger logger;
private Logger(){
}
public static Logger getLoggerInstance(){
if (logger==null){
logger=new Logger();
}
}
return logger;
}
}
A singleton class has the following
- private constructor which-prevent other classes from instantiating it directly.
- a public method with static modifier which returns the instance.--a static method can be accessed at class level without creating an object.
- private static instance of the class
package singleotons;
Public class Logger{
private static Logger logger;
private Logger(){
}
public static Logger getLoggerInstance(){
if (logger==null){
logger=new Logger();
}
}
return logger;
}
}
Comments
Post a Comment