How to define static constant fields in Kotlin?

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.

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.

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.

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 🙂

I am a very enthusiastic Android developer to build solid Android apps. I have a keen interest in developing for Android and have published apps to the Google Play Store. I always open to learning new technologies. For any help drop us a line anytime at contact@mobologicplus.com

Leave a Reply

Your email address will not be published. Required fields are marked *