Skip to content

Instantly share code, notes, and snippets.

@rishi93
Last active November 19, 2015 11:36
Show Gist options
  • Save rishi93/160a5585e71d46e502aa to your computer and use it in GitHub Desktop.
Save rishi93/160a5585e71d46e502aa to your computer and use it in GitHub Desktop.
Singleton Class using private constructor
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