9/11/2023 0 Comments Static in kotlin![]() ![]() You can give a name to your companion object like below: To get rid of the companion keyword while calling, you have to either give a name to your companion or you have to the name for the companion Val companyAddress = CompanyUtils.getCompanyAddress()īut if you want to call the same method in Java, then you have to add the companion keyword while calling the method: Method with the name of the class wherever required: Return "MindOrks, G-773, GROUND FLOOR SUNCITY, SECTOR-54 GURUGRAM, HR" In Kotlin, we can achieve the functionality of a static method by using aįor example, If you want to create a method that will return the address of your company then it is good to make this method static because for every object you create, the address is going to be the same. So, welcome to MindOrks, in this blog, we will find what is the equivalent of static methods in Kotlin. ![]() Yes, you heard it right, Kotlin doesn't have a Now, in Android development, things are moving from Java to Kotlin and one of the biggest problems that the developers face while migrating from Java to Kotlin is making a static method because in Kotlin there is nothing as such Static variables belong to a class and not to its instance. Keyword in your application to make some static variables or static methods and so on. Then you just have to import it where you need it: import Ģ.If you are an Android developer and you love to make Android applications in Java, then you must have used the Two possibilities: Declare your const in your class file (your const have a clear relation with your class) private const val CONST_USED_BY_MY_CLASS = 1Ĭreate a dedicated constants.kt file where to store those global const (Here you want to use your const widely across your project): package You just have to put your const outside your class declaration. First of all, the naming convention in Kotlin for constants is the same than in java (e.g : MY_CONST_IN_UPPERCASE). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |