java์์ Kotlin ์ฝ๋ ์ฌ์ฉํ๊ธฐ
package level function
org.example.AppKt
์๋ฐ ํด๋์ค๊ฐ ์์ฑ๋๊ณ ๋ด๋ถ static method ๋ก ์ปดํ์ผ ๋จ.
AppKt
: ํ์ผ๋ช
// app.kt
package org.example
class Util
fun getTime() { /*...*/ }
// Java
new org.example.Util();
org.example.AppKt.getTime();
@file:JvmName("DemoUtils")
: ์์ฑ๋ Java ํด๋์ค ๋ช
์ ๋ฐ๊พธ๊ณ ์ถ์ ๋ ์ฌ์ฉ
@file:JvmName("DemoUtils")
package org.example
class Util
fun getTime() { /*...*/ }
// Java
new org.example.Util();
org.example.DemoUtils.getTime();
- ๋ค์์ ํ์ผ์ ๋์ผํ
@JvmName
์ ์ค์ ํ๋ ๊ฒฝ์ฐ โ์๋ฌ ๋ฐ์ - ํ์ง๋ง
@file: JvmMultifileClass
๋ฅผ ์ฌ์ฉํ๋ฉด ํ๋์ ํด๋์ค๋ก ์์ฑ๋จ
// oldutils.kt
@file:JvmName("Utils")
@file:JvmMultifileClass
package org.example
fun getTime() { /*...*/ }
// newutils.kt
@file:JvmName("Utils")
@file:JvmMultifileClass
package org.example
fun getDate() { /*...*/ }
// Java
org.example.Utils.getTime();
org.example.Utils.getDate();
Static methods
@JvmStatic
์ฝํ๋ฆฐ์ named object ๋ companion object์ ์ ์ธ๋ ๋ฉ์๋์ ์ ๊ทผ
C.INSTANCE.callStatic()
- companion object
class C {
companion object {
@JvmStatic fun callStatic() {}
fun callNonStatic() {}
}
}
C.callStatic(); // works fine
C.callNonStatic(); // error: not a static method
C.Companion.callStatic(); // instance method remains
C.Companion.callNonStatic(); // the only way it works
- named object
object Obj {
@JvmStatic fun callStatic() {}
fun callNonStatic() {}
}
Obj.callStatic(); // works fine
Obj.callNonStatic(); // error
Obj.INSTANCE.callNonStatic(); // works, a call through the singleton instance
Obj.INSTANCE.callStatic(); // works too
*kotlin1.3 ๋ถํฐ interface ๋ด์ companion object ์๋ @JvmStatic
์ฌ์ฉ๊ฐ๋ฅ
(interface ๋ด์ static method ์ ์ธ์ java1.8๋ถํฐ ๊ฐ๋ฅ)
Instance fields
์ฝํ๋ฆฐ์์ ์์ฑ๋ค์ ๋ณดํต getter,setter๋ฅผ ํตํด ๋
ธ์ถ๋์ง๋ง, @JvmField
๋ฅผ ์ฌ์ฉํ๋ฉด ๊ฒํฐ ๋ฉ์๋ ์์ฑ์ ๊ฑด๋๋ฐ๊ณ ๋ฐฑ์
ํ๋์ ์ง์ ์ ์ผ๋ก ์ก์ธ์ค ๊ฐ๋ฅํ๋ค.
@JvmField
: ์ฝํ๋ฆฐ property ๋ฅผ ์๋ฐ์์ ํ๋๋ก ์ ๊ทผํด์ผํ๋ ๊ฒฝ์ฐ ์ฌ์ฉ
class User(id: String) {
@JvmField val ID = id
}
// Java
class JavaClient {
public String getID(User user) {
return user.ID;
}
}
Static fields
์ฝํ๋ฆฐ์ named object(singleton) ๋ companion object์ ์ ์ธ๋ ํ๋กํผํฐ์ ์ ๊ทผ
@JvmField
์ฌ์ฉ
class Key(val value: Int) {
companion object {
@JvmField
val COMPARATOR: Comparator<Key> = compareBy<Key> { it.value }
}
}
// Java
Key.COMPARATOR.compare(key1, key2);
// public static final field in Key class
- lateinit ์ผ๋ก ์ ์ธ
object Singleton {
lateinit var provider: Provider
}
// Java
Singleton.provider = new Provider();
// public static non-final field in Singleton class
const๋ก ์ ์ธ
In Kotlin, the only types that can be const are primitives, such as int, float, and String. In this case, because BACKUP_PATH is a string, we can get better performance by using const val rather than a val annotated with @JvmField, while retaining the ability to access the value as a field.
@JvmField์ ํจ๊ป ์ฌ์ฉํ val ์ด๋ ธํ ์ดํธ ๋ณด๋ค const val ๋ฅผ ์ฌ์ฉํจ์ผ๋ก์จ ๋ ์ข์ ํผํฌ๋จผ์ค๋ฅผ ๊ฐ์ง ์ ์๋ค.
// file example.kt
object Obj {
const val CONST = 1
}
class C {
companion object {
const val VERSION = 9
}
}
const val MAX = 239
int const = Obj.CONST;
int max = ExampleKt.MAX;
int version = C.VERSION;
interface์ default method ์ฌ์ฉ
- compile with
-Xjvm-default=all
// compile with -Xjvm-default=all
interface Robot {
fun move() { println("~walking~") } // will be default in the Java interface
fun speak(): Unit
}
//Java implementation
public class C3PO implements Robot {
// move() implementation from Robot is available implicitly
@Override
public void speak() {
System.out.println("I beg your pardon, sir");
}
}
//Java
public class BB8 implements Robot {
//own implementation of the default method
@Override
public void move() {
System.out.println("~rolling~");
}
@Override
public void speak() {
System.out.println("Beep-beep");
}
}