111 ms·
val field is a shorter way of saying getField, actually, not final field, and that's the point of the article. Kotlin just doesn't have fields in Java sense (th
by vbezhenar 9y ago
val field is a shorter way of saying getField, actually, not final field, and that's the point of the article. Kotlin just doesn't have fields in Java sense (though it uses them, of course), for programmer they are properties, AKA getter+setter with trivial getter/setter generated automatically.
- jorgemf 9y agoThe article talks about immutable objects vs read-only objects. Final and Val means read only, the reference is immutable, not the object. For class variables Val and final are almost exactly the same.
- vbezhenar 9y agoval reference is not immutable. Your val can return new reference every time it's called: val x: String get() = (i++).toString() And JVM just don't have any notion about immutable objects, so Kotlin, obviously, don't have them either. You can return some ImmutableList and I'll change it using reflection and laugh at you. Immutability in Java is about conventions and trust.
- jorgemf 9y agoI said Val means read only, not immutable. I never said java have immutability (only for primitive values)
- swsieber 9y agoThough, you can add an annotation to get field behavior - it's mainly for java compatibility from what I can tell (and it's documented in the java interop section). I'm not sure if using that annotation also generates the getters/setters though.