June 2018
Intermediate to advanced
316 pages
6h 34m
English
In Kotlin, we can declare variables and methods as top-level members, but not inside a class body. Let's look at the following example:
const val compileTime: Int = 5fun compileTimeFunction() = compileTime + compileTime
This code, when decompiled to Java, looks as follows:
public final class MainKt { public static final int compileTime = 5; public static final int compileTimeFunction() { return 10; }}
There's no overhead here. The MainKt class just contains two static members without <cinit>()V or static <clinit>()V methods. This is because we use the const modifier.
Read now
Unlock full access