Skip to content

Instantly share code, notes, and snippets.

@yenerm
Last active April 22, 2020 04:27
Show Gist options
  • Save yenerm/a0d189d173665eb72b407e2d59689fe8 to your computer and use it in GitHub Desktop.
Save yenerm/a0d189d173665eb72b407e2d59689fe8 to your computer and use it in GitHub Desktop.
companion object
<!-- Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
class SomeClass {
//…
companion object {
private var count: Int = 0
fun count() {
count++
}
}
}
class AnotherClass {
//…
companion object Counter {
private var count: Int = 0
fun count() {
count++
}
}
}
// usage without name
SomeClass.count()
// usage with name
AnotherClass.Counter.count()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment