In Kotlin, properties are declared using the val
and var
keywords. val
is used for read-only properties (immutable), while var
is used for mutable properties. Here is a simple example:
// Read-only property
val name: String = "John"
// Mutable property
var age: Int = 30
Kotlin does not have traditional fields like Java. Instead, it uses backing fields implicitly when needed. A backing field is used to store the value of a property. You can access the backing field using the field
keyword inside a custom getter or setter.
class Person {
var age: Int = 0
set(value) {
if (value >= 0) {
field = value
}
}
}
In this example, the field
keyword is used to store the value of the age
property after validating it.
You can define custom getters and setters for properties to control how the property is accessed and modified. Here is an example of a custom getter:
class Rectangle(val width: Int, val height: Int) {
val area: Int
get() = width * height
}
And here is an example of a custom setter:
class Temperature {
var celsius: Double = 0.0
set(value) {
field = value
fahrenheit = value * 1.8 + 32
}
var fahrenheit: Double = 32.0
}
Immutable properties are useful when you want to ensure that a value does not change after it is initialized. They are declared using the val
keyword.
class Circle(val radius: Double) {
val area: Double
get() = Math.PI * radius * radius
}
In this example, the radius
property is immutable, and the area
property is a computed property that depends on the radius
.
Mutable properties are used when you need to change the value of a property. They are declared using the var
keyword.
class Counter {
var count: Int = 0
set(value) {
if (value >= 0) {
field = value
}
}
fun increment() {
count++
}
}
Computed properties are properties whose value is calculated based on other properties. They do not have a backing field and are defined using a custom getter.
class Point(val x: Int, val y: Int) {
val distanceFromOrigin: Double
get() = Math.sqrt(x * x + y * y)
}
Getters and setters should be simple and perform only basic operations. Avoid adding complex logic inside getters and setters, as it can make the code harder to understand and maintain.
val
for Immutable PropertiesWhenever possible, use the val
keyword for properties that do not need to change. This makes the code more predictable and easier to reason about.
Custom getters and setters should be used only when necessary, such as when you need to perform validation or calculation. Overusing them can lead to unnecessary complexity in the code.
Kotlin’s property access syntax is a powerful and flexible feature that allows you to work with properties in a concise and efficient way. By understanding the core concepts, typical usage scenarios, and best practices, you can write cleaner, more maintainable code. Remember to keep your getters and setters simple, use val
for immutable properties, and avoid overusing custom getters and setters.