this is my code:
var header1: Record? = null
var header2: Record? = null
header2 = header1
header2.name = "new_name"
but header1.name
changes too!
You are just assigning the same object (same chunk of memory) to another variable. You need to somehow create a new instance and set all fields.
header2 = Record()
header2.name = header1.name
However in Kotlin, if the Record class was Data class, Kotlin would create a copy method for you.
data class Record(val name: String, ...)
...
header2 = header1.copy()
And copy method allows you to override the fields you need to override.
header2 = header1.copy(name = "new_name")
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments