Skip to content

Instantly share code, notes, and snippets.

@CoderJava
Forked from ademar111190/singletonExample.java
Created July 16, 2017 05:00
Show Gist options
  • Save CoderJava/7435cb75ab8bec8e755ee50f6755cddb to your computer and use it in GitHub Desktop.
Save CoderJava/7435cb75ab8bec8e755ee50f6755cddb to your computer and use it in GitHub Desktop.
One month with Kotlin: singleton example
// Using Singleton on Kotlin
public object MySingleton {
public fun foo() {
}
}
// And use it on Kotlin
MySingleton.foo()
//------------------------------------------------------------------------------
// Using Singletons in Java 7
public class MySingleton {
private static MySingleton instance;
private MySingleton() {
}
public static MySingleton getInstance(){
if(instance == null) {
instance = new MySingleton();
}
return instance;
}
public void foo() {
}
}
//Using use it in Java
MySingleton.getInstance().foo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment