Created
November 14, 2011 17:32
-
-
Save zeeshanlakhani/1364543 to your computer and use it in GitHub Desktop.
Simple Singleton Example in Java
This file contains 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.*; | |
import java.util.*; | |
class Singleton { | |
private static Singleton _Instance; | |
private Singleton() | |
{ | |
} | |
public static Singleton GetInstance() | |
{ | |
if (_Instance == null) | |
_Instance = new Singleton(); | |
return _Instance; | |
} | |
public void PrintHello() | |
{ | |
System.out.println("Hello"); | |
} | |
} | |
public class TestSing { | |
public static void main (String args[]){ | |
Singleton s = Singleton.GetInstance(); | |
//SAME INSTANCE | |
Singleton a = Singleton.GetInstance(); | |
s.PrintHello(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment