
1. Overview
In this article, we will see the differences between Kotlin field vs property.
A field and property are member variables of a class. Let’s first understand the member variables. You can skip the below section on member variables if you are already aware.
2. Kotlin member variables
The member \ instance variables are variables declared inside a class but outside the class method. The scope is class level and thus accessible from all non-static methods declared within the class. Based on its access modifier, it can be accessible from outside the class as well.
Let’s take the below example. title
and desc
are the member or instance variables, whereas the displayText
inside the display
method is a local variable and is not an instance variable.
The member variables title
and desc
are accessible directly inside the non-static method display
.
class Alert() { var title: String? = null var desc: String? = null constructor(title: String, desc: String) : this() { this.title = title this.desc = desc } fun display() { var displayText = title + desc println(displayText) } } fun main() { val alert = Alert("Alert!!!\n", "Do you want to continue?") alert.display() }
3. Kotlin property vs field
Now let’s find out the differences between a property and a field. The difference is mostly a matter of terminology. Kotlin has property whereas Java has fields.
The below name
is a member variable in Kotlin. By default, the Kotlin compiler auto-generates getter (and setters for mutable properties) accessors for member variables. However, you can also provide your own custom getters or setters. This member variable is a property.
In short, a property is a member variable with getter \ setter accessors.
var name = "Praj" /* Kotlin property and has default getter \ setters */
Below code is the Java equivalent of the above Kotlin property. If you have prior knowledge of Java, you might know that the setter and getter methods need to be added manually for the variables and are not auto-generated by the compiler.
The below name
is a field in Java.
private String name = "Praj"; public String getName() { return name; } public void setName(String name) { this.name = name; }
A field is a concept in Java where a class member variable holds a value and doesn’t have any getters or setters.
A property with no getter or setter is the field. Thus, you can consider it as a part of the property and not a separate thing from a property. And a field is used to store the value in memory.
A property = A field + getter \ setter accessors
You cannot declare a field directly in Kotlin classes. Because the compiler generates a getter or setter automatically for the variable. However, you can annotate the property with @JvmField to make it a Java field. The compiler won’t generate getter \ setter for the properties annotated with @JvmField.
The same name
field in Java doesn’t have any getter or setters and looks like below:
public String name = "Praj";
4. Conclusion
To sum up, we have learned the Kotlin member variables concept. And also discussed the differences between the field vs property.
1 thought on “Kotlin field vs property”