1. A Hungry man type
public class download{
private static final DownLoad mdownload =new DownLoad ();
Private Download () {
}
public static DownLoad getinstance () {
return mdownload;
}
}
2. Lazy Type
public class download{
private static final DownLoad mdownload;
Private Download () {
}
public static DownLoad getinstance () {
if (Mdownload ==null) {
Mdownload =new DownLoad ();
}
return mdownload;
}
}
3.double check (null pointer issues can be resolved by adding the volatile keyword)
public class download{
private static volatile DownLoad mdownload;
Private Download () {
}
public static DownLoad getinstance () {
if (Mdownload ==null) {
Synchronized (Download.class) {
if (mdownload) {
Mdownload =new DownLoad ();
}
}
}
return mdownload;
}
}
4. Static Inner class
public class download{
public static class{
private static DownLoad mdownload=new DownLoad ();
public static DownLoad getinstance () {
return mdownload;
}
}
}
Four ways to style Java Singleton