Properties

A property is just a combination of a backing field and its accessors. It could be a backing field with both a getter and a setter or a backing field with only one of them. Properties can be defined at the top level (directly inside the file) or as a member (for example, inside the class or interface).

In general, it is advisable to define properties (private fields with getters/setters) instead of accessing public fields directly (according to Effective Java, by Joshua Bloch, item 14: in public classes, use accessor methods, not public fields).

Java getter and setter conventions for private fields Getter: A parameterless method with a name that corresponds to the property name and a get prefix (for a Boolean property there might ...

Get Android Development with Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.