Ideas are easy. Implementation is hard. Be Passionate.

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.

1

2

3

4

5

6

7

8

9

publicclassConstant{

publicstaticfinalStringTABLE_NAME="friends";

publicstaticfinalStringID="_id";

publicstaticfinalStringFRIEND_ID="friend_id";

publicstaticfinalStringNAME="name";

publicstaticfinalStringEMAIL="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.

1

2

3

4

5

6

companionobject{

@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.

1

2

3

4

5

6

7

8

objectFriendsTable{

val TABLE_NAME="friends"

val ID="_id"

varFRIEND_ID="friend_id"

varNAME="name"

varEMAIL="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.

Here are the best offer and recommendation for you to download this. 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.

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

Categories

For better assist

Hey Folks, Welcome to the MobologicPlus blog. I’m Sunil Gupta, a tech blogger from Bangalore, India.
I started MobologicPlus as a passion, by helping people to understand the technical kinds of stuff from this blog. You have some feedback for MobologicPlus. Where should I send it? Sure, drop us a line anytime at contact@mobologicplus.com