What is the equivalent of Java static methods in Kotlin?
What is the equivalent of Java static methods in Kotlin?
In Kotlin, there are a few ways to achieve the functionality of Java static methods:
class CompanyUtils {
companion object {
fun getCompanyAddress(): String {
return "MindOrks, G-773, GROUND FLOOR SUNCITY, SECTOR-54 GURUGRAM, HR"
}
}
}
val companyAddress = CompanyUtils.getCompanyAddress()
@JvmStatic annotation: You can annotate functions inside a companion object or object declaration with @JvmStatic to generate static methods in the enclosing class. This allows calling the methods directly using the class name in both Kotlin and Java.Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.