When we try to use Kotlin as a development language, we should think about how to define a constant in Kotlin, as in Java code like this


1
Copy the code
Public static final double PI = 3.14159265358979323846;Copy the code

In Kotlin, two simple keywords are provided, one var and the other val

  • Var stands for ordinary mutable variables, readable and writable
  • Val is represented as a read-only variable. The Java equivalent of a final variable

1, 2, 3, 4, 5, 6, 7, 8Copy the code
var title: String? = null

val id: Long = 1L

fun test() {
    title = "Title set in test function"
    id = 111 //compile error
}
Copy the code

So the val modifier is Kotlin’s constant

Many people have mistakenly assumed that val is a constant in Kotlin, and then it is not, as in the following implementation


1
2
Copy the code
val currentTimeMillis: Long
    get() {return System.currentTimeMillis()}
Copy the code

The value we get above currentTimeMillis is changing every time we access it, so it’s not constant.

Why?

This is because in Kotlin, a var generates two corresponding methods, getter and setter methods, for example


1
Copy the code
var title: String? = null
Copy the code

The resulting bytecode will contain the following two methods and a backing field


1, 2, 3, 4, 5, 6, 7, 8, 9Copy the code
private static String title;
@Nullable
public static final String getTitle() {
  return title;
}

public static final void setTitle(@Nullable String title) {
    title = title;
}
Copy the code

For val, only one corresponding GET method is generated, for example


1
Copy the code
val id: Long = 1L
Copy the code

The generated bytecode will contain methods like this


1
2
3
4
Copy the code
private static final long id = 1L;
public static final long getId() {
    return id;
}
Copy the code

How do you generate true constants

There are two ways to implement true constants: one is const, and the other is using the @jVMField annotation

const

Const, as its name implies, is short for constant. You can use it to declare constants, but only in top-level and object.


1, 2, 3, 4, 5, 6, 7, 8, 9Copy the code
//top level const val name = "Kotlin" //object class DemoConstant {companion object {const val subName = ""}}Copy the code
  • A top-level is the outermost part of the code file, such as common classes (non-inner and nested) at the top level. Structurally constants do not belong to any class, but to files.
  • Object can refer to the most external object or to the companion object.

@JvmField

  • A val constant can be made constant by adding an @jVMField in front of it.
  • Its internal effect is to inhibit the compiler from generating the corresponding getter method
  • The get method of val cannot be overridden if it is decorated with this annotation

The sample is as follows


1
Copy the code
@JvmField val NAME = "89757
Copy the code

As for Kotlin’s constant study, the most effective method is to analyze bytecode and decomcompile comparative learning. For more information on how to learn Kotlin you can read this article to study some ways to learn Kotlin