Are public attributes rather than private attributes with get/set methods common practice?

Okay, I think I got it.

Lets assume I have two classes:

class User{
    var uid: Int = 0
    var name: String = ""
    var email: String = ""

    fun login(){}
    fun logout(){}
    fun deleteAccount(): Boolean{ return true }
}

class UserManager{
    private val newUser = User()

    fun createUser(){
        newUser.uid = 1
        newUser.name = "First User"
        newUser.email = "first@user.de"
    }

    fun deleteUser(uid: Int = 0) {
    }
}

Would it be okay to have these type of properties defined as public, both from a performance and a security standpoint?

TL/DR: Whenever I need access to properties outside of the class, always make them public and use them directly by calling the property like Object.propertyname = “TEST”. If I dont need to access them from outside of the class, keep them private/protected.

Really appreciate all the effort you guys have put into explaining this to me!

1 Like