์ถ์ฒ
https://thinking-face.tistory.com/185
https://kotlinlang.org/docs/properties.html#compile-time-constants
https://stackoverflow.com/questions/37595936/what-is-the-difference-between-const-and-val
const ์์ ์
-> ์ปดํ์ผ ํ์ ์์. ์ปดํ์ผ ์๊ฐ์ ์ฝ๊ธฐ ์ ์ฉ์ผ๋ก ๊ฐ์ด ํ ๋น๋๋ค.
- ์ต์๋จ ํ๋กํผํฐ, object ์ ์ธ์ ๋ฉค๋ฒ, companion object์ ๋ฉค๋ฒ๋ก๋ง ์ธ ์ ์๋ค.
- ์์ ํ์ ๊ณผ String ํ์ ์ผ๋ก๋ง ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
- ์ปค์คํ ๊ฒํฐ ๊ตฌํ์ด ๋ถ๊ฐ๋ฅํ๋ค.
์์
1. ์ต์๋จ
์ฝํ๋ฆฐ ์ฝ๋
val apple = "apple"
const val pear = "pear"
-> ์๋ฐ ๋ณํ ์ฝ๋
@Metadata(
mv = {1, 7, 0},
k = 2,
d1 = {"\u0000\n\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0004\"\u0014\u0010\u0000\u001a\u00020\u0001X\u0086D¢\u0006\b\n\u0000\u001a\u0004\b\u0002\u0010\u0003\"\u000e\u0010\u0004\u001a\u00020\u0001X\u0086T¢\u0006\u0002\n\u0000¨\u0006\u0005"},
d2 = {"apple", "", "getApple", "()Ljava/lang/String;", "pear", "MyCodeForCodingTest"}
)
public final class FruitKt {
@NotNull
private static final String apple = "apple";
@NotNull
public static final String pear = "pear";
@NotNull
public static final String getApple() {
return apple;
}
}
2. object
์ฝํ๋ฆฐ ์ฝ๋
object Orange {
val pineapple = "pineapple"
const val cherry = "cherry"
}
-> ์๋ฐ ๋ณํ ์ฝ๋
// Orange.java
import kotlin.Metadata;
import org.jetbrains.annotations.NotNull;
@Metadata(
mv = {1, 7, 0},
k = 1,
d1 = {"\u0000\u0014\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0004\bÆ\u0002\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002R\u000e\u0010\u0003\u001a\u00020\u0004X\u0086T¢\u0006\u0002\n\u0000R\u0014\u0010\u0005\u001a\u00020\u0004X\u0086D¢\u0006\b\n\u0000\u001a\u0004\b\u0006\u0010\u0007¨\u0006\b"},
d2 = {"LOrange;", "", "()V", "cherry", "", "pineapple", "getPineapple", "()Ljava/lang/String;", "MyCodeForCodingTest"}
)
public final class Orange {
@NotNull
private static final String pineapple;
@NotNull
public static final String cherry = "cherry";
@NotNull
public static final Orange INSTANCE;
@NotNull
public final String getPineapple() {
return pineapple;
}
private Orange() {
}
static {
Orange var0 = new Orange();
INSTANCE = var0;
pineapple = "pineapple";
}
}
3. companion object
์ฝํ๋ฆฐ ์ฝ๋
class Grape {
val banana = "banana"
// const val plum = "plum" ์ ์ธ ๋ถ๊ฐ
companion object {
val watermelon = "watermelon"
const val mango = "mange"
}
}
-> ์๋ฐ ๋ณํ ์ฝ๋
// Grape.java
import kotlin.Metadata;
import kotlin.jvm.internal.DefaultConstructorMarker;
import org.jetbrains.annotations.NotNull;
@Metadata(
mv = {1, 7, 0},
k = 1,
d1 = {"\u0000\u0014\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0004\u0018\u0000 \u00072\u00020\u0001:\u0001\u0007B\u0005¢\u0006\u0002\u0010\u0002R\u0014\u0010\u0003\u001a\u00020\u0004X\u0086D¢\u0006\b\n\u0000\u001a\u0004\b\u0005\u0010\u0006¨\u0006\b"},
d2 = {"LGrape;", "", "()V", "banana", "", "getBanana", "()Ljava/lang/String;", "Companion", "MyCodeForCodingTest"}
)
public final class Grape {
@NotNull
private final String banana = "banana";
@NotNull
private static final String watermelon = "watermelon";
@NotNull
public static final String mango = "mange";
@NotNull
public static final Companion Companion = new Companion((DefaultConstructorMarker)null);
@NotNull
public final String getBanana() {
return this.banana;
}
@Metadata(
mv = {1, 7, 0},
k = 1,
d1 = {"\u0000\u0014\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0004\b\u0086\u0003\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002R\u000e\u0010\u0003\u001a\u00020\u0004X\u0086T¢\u0006\u0002\n\u0000R\u0014\u0010\u0005\u001a\u00020\u0004X\u0086D¢\u0006\b\n\u0000\u001a\u0004\b\u0006\u0010\u0007¨\u0006\b"},
d2 = {"LGrape$Companion;", "", "()V", "mango", "", "watermelon", "getWatermelon", "()Ljava/lang/String;", "MyCodeForCodingTest"}
)
public static final class Companion {
@NotNull
public final String getWatermelon() {
return Grape.watermelon;
}
private Companion() {
}
// $FF: synthetic method
public Companion(DefaultConstructorMarker $constructor_marker) {
this();
}
}
}
'๋น ๊ตฌ๋ฉ ์ฑ์ฐ๊ธฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JVM] invokedynamic ์ดํดํ๊ธฐ (0) | 2024.11.01 |
---|---|
[Kotlin] interface ์ default method๋ฅผ ์๋ฐ๋ก ๋ณํํ๊ธฐ (2) | 2024.10.29 |
[Kotlin] @JvmOverloads - ๋ํดํธ ํ๋ผ๋ฏธํฐ๋ฅผ ์๋ฐ์์๋ ์ด์ฉํ๊ธฐ (0) | 2024.10.29 |
[Kotlin] @JvmField : ์ปดํ์ผ์ ๊ฒํฐ, ์ธํฐ ์์ ๊ธฐ (1) | 2024.10.29 |
[Kotlin][Java] Kotlin ๋ฌธ์์ด ์ฐ๊ฒฐ์ด ์๋ฐ๋ก ์ปดํ์ผ ์์ ๋์ (3) | 2024.10.29 |