Last active
December 18, 2015 01:09
-
-
Save charlierm/5702189 to your computer and use it in GitHub Desktop.
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
public class main{ | |
public static void main(String[] args) { | |
//WORKS | |
SingletonDemo s = SingletonDemo.getInstance(); | |
s.setValue(12); | |
//FAILS | |
SingletonDemo ss = SingletonDemo.getInstance(); | |
System.out.println(ss.getValue()); | |
} | |
} | |
class SingletonDemo { | |
private static volatile SingletonDemo instance = null; | |
private int value; | |
private SingletonDemo() { } | |
public static SingletonDemo getInstance() { | |
if (instance == null) { | |
instance = new SingletonDemo (); | |
} | |
return instance; | |
} | |
public void setValue(int value){ | |
this.value = value; | |
} | |
public int getValue(){ | |
return this.value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment