Static implementation of Kotlin
In Java, static properties or methods can be declared using the static keyword. Instead of continuing this keyword, Kotlin uses the companion object implementation to declare a Companion Object block within the class, whose member variables and methods will be compiled statically.
Class TestStatic {// Companion object Factory {val STR: String =""
fun create(): TestStatic {
println(this)
return TestStatic()
}
}
}
Copy the code
Factory is the class name of the resulting static inner class. Normally, the Factory name can be omitted, and if omitted, the default class name is Companion.
Call a method just like calling a static method class name + method name.
TestStatic.create()
Copy the code
Static code block
It is also possible to write static blocks in Kotlin by nesting an init block in the Companion Object.
Companion object {static block init {val a ="adc"}}Copy the code
Decompiles the code to see that it is written in Java.
static {
String var0 = "adc";
}
Copy the code
The difference from the internal Object class
We can take a look at the internal implementation by decompiling.
Public final class TestStatic {private static final String STR ="";
public static final TestStatic.Factory Factory = new TestStatic.Factory();
public static final class Factory {
@NotNull
public final String getStr() {
return TestStatic.str;
}
@NotNull
public final TestStatic create() {
System.out.println(this);
return new TestStatic();
}
private Factory() {}}}Copy the code
Does it feel a bit like the Object class in the last article? In fact, it is similar to all but one keyword, so we will remove the companion keyword and decompile it for comparison.
class TestStatic {
object Factory {
val str: String = ""
fun create(): TestStatic {
println(this)
return TestStatic()
}
}
}
Copy the code
Decompilation result:
Public final class TestStatic {public static final class Factory {private static final String STR ="";
public static final TestStatic.Factory INSTANCE;
@NotNull
public final String getStr() {
return str;
}
@NotNull
public final TestStatic create() {
System.out.println(this);
return new TestStatic();
}
private Factory() {
}
static {
TestStatic.Factory var0 = new TestStatic.Factory();
INSTANCE = var0;
str = ""; }}}Copy the code
The code comparison shows a clear difference. Although both will eventually generate a static inner class, the member variables in the Companion Object block will eventually be declared in the outer class, which is why we can access it directly by the outer class name, whereas the Object class requires the outer class. Inner class.
/ / object class call way TestStatic. Factory. The create () / / companion object code block use TestStatic. The create (); / / the compiler can we auto-completion for TestStatic Factory. The create ()Copy the code
Matters needing attention
- There can be at most one Companion Object block in a class.
- The associated object is essentially a static inner class, so it can also inherit from other classes, as follows.
Companion Object: Comparable<String> {override fun compareTo(other: String): Int {Copy the code