Skip to content

Instantly share code, notes, and snippets.

@yenerm
Last active May 4, 2020 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yenerm/441d338180dab984caf1e64e4a220be0 to your computer and use it in GitHub Desktop.
Save yenerm/441d338180dab984caf1e64e4a220be0 to your computer and use it in GitHub Desktop.
Kotlin singleton converted from Java
<!-- Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
class Singleton private constructor() {
private var count = 0
fun count(): Int {
return count++
}
companion object {
private var INSTANCE: Singleton? = null// Double checked
// Single Checked
val instance: Singleton?
get() {
if (INSTANCE == null) { // Single Checked
synchronized(Singleton::class.java) {
if (INSTANCE == null) { // Double checked
INSTANCE =
Singleton()
}
}
}
return INSTANCE
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment