Java Program To Make Singleton Class
Chapter:
Interview Programs
Last Updated:
19-07-2016 13:45:18 UTC
Program:
/* ............... START ............... */
public class JavaSingletonClass {
private static JavaSingletonClass singletonObj;
static {
singletonObj = new JavaSingletonClass();
}
private JavaSingletonClass() {
}
public static JavaSingletonClass getInstance() {
return singletonObj;
}
public void welcome() {
System.out.println("Welcome to JavaScan.com");
}
public static void main(String a[]) {
JavaSingletonClass ms = getInstance();
ms.welcome();
}
}
/* ............... END ............... */
Output
Notes:
-
Singleton class means you can create only one object for the given class.
- You can create a singleton class by making its constructor as private, so that you can restrict the creation of the object.
Tags
Singleton Class, Java