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

Yeah I’m sorry I didn’t clarify more. myExample.myAmazingValue = "123" will translate to myExample.setMyAmazingValue("123") in the resulting compiled java bytecode. Furthermore, System.out.println(myExample.myAmazingValue) will translate into System.out.println(myExample.getMyAmazingValue()) in the java bytecode. The getter and setter methods are only visible from java to preserve compatibility, but there’s no reason whatsoever to use them in Kotlin, and so they are hidden and instead you just use the normal property access syntax.

Although it looks like you are accessing the attribute directly, it’s just syntatic sugar for the actual method call, and so if myAmazingValue had a custom getter, then that custom getter will get called (i.e. you wouldn’t directly get the value from the private field _myAmazingValue, you’ll get the value from the function getMyAmazingValue.)

1 Like