Junior: ASenior: Kotlin is on pretty solid footing. Now, in Java also, we can access the fields of the class in the same way as in Kotlin: RetryWithDelay retryOne = new RetryWithDelay(3, 1000) int maxNumberOfRetries = retryOne. So, the Kotlin code after using the annotation will be: data class val maxRetriesCount: Int, private val retryDelayInMillis: Int = 2000) Namely fields that are only created once per class and can be accessed without an instance of their containing class. So, if we want a particular field to be used as normal field and not as getter or setter then we have to tell the compiler not to generate any getter and setter for the same and this can be done by using the annotation. Kotlin programming language Most of the programming language have concepts where classes can have static members. maxRetriesCountīut in Java, we have to use the getter method: RetryWithDelay retryOne = new RetryWithDelay(3, 1000) int maxNumberOfRetries = retryOne. So, if we are accessing the properties of the class then the code in Kotlin is: val retryOne = RetryWithDelay(3, 1000) val maxNumberOfRetries = retryOne. Public class RetryWithDelay Īnd the Kotlin code for the same will be: data class RetryWithDelay( private val maxRetriesCount: Int, private val retryDelayInMillis: Int = 2000) Lead: Let’s take the RetryWithDelay Pojo class in our project where it helps in retrying the same API(when it fails) with a given number of times and specified time gap in milliseconds.Įg : Fetch Students List 3( maxRetriesCount) times and with time gap 2( retryDelayInMillis) seconds Lead: Kotlin is best because we can write the same in a much precise way having a lesser number of lines of code (there are other benefits too) Junior: Lead, Please explain why we are migrating our project to Kotlin from Java? The conversation between Junior and Lead Android Developer goes on like this…………
0 Comments
Leave a Reply. |