-
-
Save CoderJava/7435cb75ab8bec8e755ee50f6755cddb to your computer and use it in GitHub Desktop.
One month with Kotlin: singleton example
This file contains hidden or 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
// 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