Skip to content

Instantly share code, notes, and snippets.

@charlierm
Last active December 18, 2015 01:09
Show Gist options
  • Save charlierm/5702189 to your computer and use it in GitHub Desktop.
Save charlierm/5702189 to your computer and use it in GitHub Desktop.
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