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 be an
is prefix instead).Setter: Single-argument methods with names starting with
set: for example,
Kotlin guards this principle by language design, because this approach provides various encapsulation benefits:
- The ability...