Last active
November 19, 2015 11:36
-
-
Save rishi93/160a5585e71d46e502aa to your computer and use it in GitHub Desktop.
Singleton Class using private constructor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
class singletonExample | |
{ | |
public static singletonExample obj = null; | |
private singletonExample() | |
{ | |
System.out.println("Created object"); | |
} | |
public static singletonExample createObject() | |
{ | |
if(obj == null) | |
{ | |
obj = new singletonExample(); | |
} | |
return obj; | |
} | |
} | |
public class test | |
{ | |
public static void main(String args[]) | |
{ | |
singletonExample ob1 = singletonExample.createObject(); | |
singletonExample ob2 = singletonExample.createObject(); | |
singletonExample ob3 = singletonExample.createObject(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment