The question is in the title: In Kotlin, if an object is passed into a new instance of a class and then some properties are changed, will it change the properties in the original object? Image I've got the following situation:
class User{
var name: String = "initial name"
}
class UserHolder{
companion object{
var INSTANCE: User
}
}
class ClassA{
fun doStuff(){
val classB = ClassB(UserHolder.INSTANCE)
classB.changeUser()
val newName = UserHolder.INSTANCE.name // is it "initial name" or "My new name"?
}
}
class ClassB(private val user: User){
fun changeUser(){
user.name = "My new name"
}
}
will the newName be is "initial name" or "My new name" and why? I know that if I pass an object into a method and change its properties, then on return the properties will be updated. But why doesn't it work with the classes? Is it because we write 'val user'?