'Is the companion object value of the class different from the companion object value of the instance?
I'm new to Kotlin and I'm learning.
I wonder if the value of companion object of class and value of companion object of created instance are separate.
I want to change the value of companion object in a list instance created with class Person using companion object.
Below is the example code.
fun main() {
val personlist : List<Person> = listOf(Person(1), Person(2), Person(3))
Person.name = "ABC" // Will the name of the personlist change?
personlist.name = "ABC" // error
}
class Person(val num: Int) {
companion object {
var name: String = "TEST"
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
