How to define static constant fields in Kotlin?

Sharing is caring!

While developing any android application we need to define the bunch of constant fields and variable into the Constant file. As we remembered while building Android application by using Java language, we used Constant.java file to declare all the static fields.

Let’s give one example to understand that how we are defining the constant fields and variables in a Constant file.

public class Constant {

    public static final String TABLE_NAME = "friends";
    public static final String ID = "_id";
    public static final String FRIEND_ID = "friend_id";
    public static final String NAME = "name";
    public static final String EMAIL = "email";

}

Whenever we want to use any fields then it is very easy to call, for example, Constant.TABLE_NAME. Let’s see how the same thing we can do in Kotlin?

As we practiced in my last tutorials, we have seen many places that Koltin removed boilerplate code. Kotlin makes it easy to express complex things with simple code, with compiler doing the dirty work. The best example of boilerplate code is data class. As we know to defile any POJO class in Java then we need to add so many lines of code in this Class. Kotlin removed all these codes and make it pretty easy and simple in just one single line.

If you want to know more details of Kotlin Classes then Please check this post to better understand. There’s no static keyword in Kotlin. If you want static access to certain fields or methods of your class, you should put them under a companion object.

 companion object {
        @JvmField
        val DB_VERSION = 1
        @JvmField
        val DB_NAME = "dbFriends"
    }

The field will be available globally and accessible through Constants.DB_VERSION. Kotlin properties declared in a named object or a companion object will have static backing fields either in that named object or in the class containing the companion object.

We can be exposed the static fields in three different way as a private in Kotlin.

  • @JvmField annotation
  • lateinit modifier
  • const modifier

@JvmField instructs the Kotlin compiler not to generate getters/setters for this property and expose it as a field. Late initialized properties are also exposed as fields. The visibility of the field will be the same as the visibility of propertylateinit setter. const is that it only works with primitives and Strings.

Great 🙂 So this is an example of companion object to define static fields in the constant file in Kotlin. Now let’s see the other way by using only object keyword to define the constant fields.

object FriendsTable{

    val TABLE_NAME = "friends"
    val ID = "_id"
    var FRIEND_ID = "friend_id"
    var NAME = "name"
    var EMAIL = "email"
}

All fields will be available globally and accessible through FriendsTable.TABLE_NAME etc.

Wrapping up: As we have seen that Kotlin does not have a static keyword, but we can define the constant fields very easy in Kotlin to access those fields as globally. Kotlin has removed all the repeated or boilerplate code. It makes quite easy and simple for the developers to use this language. Please check this official blog post about static constant.

Please do subscribe your email to get the updated newsletters from this blog and if you feel that this post will help you to understand then do not forget to share and comment below.

Happy Coding 🙂

0 0 votes
Article Rating
How to define static constant fields in Kotlin?
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Scroll to top
0
Would love your thoughts, please comment.x
()
x