Kotlin foundation -- static methods, static variables and constants

In Java, we usually use static variables and static methods as tool classes.

public class StaticUtils {

    public static String staticProperty;

    public static String getStaticProperty(){
        return staticProperty;


How to call the methods and variables of the class directly with the class name in Kotlin? Let's first look at the role of the object keyword.


Like class, Object is a keyword and o must be lowercase. This is different from Java Object. Any plays the same role as Java Object in Kotlin. It is the base class of all classes.
Object can directly call variables and methods by class name in Kotlin. The function is similar to the static keyword of Java. But the difference is that the object is placed before the class name.

object KotlinStaticUtils {
    val name: String = "name"
    fun setAge(){

//It is called directly through the class name where it is called
 val name = KotlinStaticUtils.name


In Kotlin, object kotlinstatictutils is also a thread safe singleton class.

Inheritance and Implementation

The class of object can inherit other classes and implement interfaces at the same time.

open class BaseUtils {

    open fun baseMethod(){

interface BaseInterfaceUtils {
    fun baseInterfaceMethod()
object KotlinStaticUtils : BaseUtils() , BaseInterfaceUtils{

    override fun baseMethod() {
        println("baseMethod children")

    override fun baseInterfaceMethod() {


However, the class modified by object cannot be inherited by other classes

Anonymous class

How to write anonymous classes:

       val baseAbstract = object : BaseAbstract(){
            override fun baseAbstract() {

        val baseIntercae = object : BaseIntercae{
            override fun onClick(v: View) {

Some functions and variables are static

All variables and functions in the object class are static. To achieve that local functions and variables are static, you can write this:

class Constants {
    object A {
        var tag = "Constants"

A is the nested class of the Constants class, and the class can have multiple nested classes. When calling, you can directly call the functions and variables in a through the class name.


You can omit A when calling through the following writing method

class Constants {

    companion object {
        var methodName = "Associated class"

The object class modified by the company is called an associated class. There can only be one associated class in each class. Constants.methodName can be called directly through the class name.

top-level property or function

Top level function, top level variable (self for convenience of memory).

Top level belongs to package, not class. Like static variables, it can act globally.

package com.example.kotlindemo.toplevel

 * @author qinhaihang
 * @time   2019/11/26 23:42
 * @des
 * @packgename com.example.kotlindemo.toplevel
var COUNT: Int = 0

fun addOne(data: Int): Int {
    return data + 1

It can then be called directly elsewhere

for (i in 1 until 10){
        val data = addOne(2)
        Log.d("qq","data = $data")

Difference between obejct and top level

  1. Top level is more suitable for tool classes
  2. If implementation and inheritance are required, object and company are more appropriate


Constants in Kotlin cannot be modified at all. They can only be basic types and strings. The three commonly used expressions are as follows:

//Declare in top level
package com.example.kotlindemo.staticdemo

 * @author qinhaihang
 * @time   2019/11/27 22:55
 * @des
 * @packgename com.example.kotlindemo.staticdemo
const val NUMBER_3 = 3

//Associated class declaration in class
class Constants2 {

    companion object{
        const val NUMBER_1 = 1


//Declare in object class
object Constants {

    const val NUMBER_1: Int = 0


The above are some common summaries. I also refer to the parabola article and the article on the official website

Author: HNA 666
Link: https://www.jianshu.com/p/19779eba4a0c
Source: Jianshu
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.

Tags: kotlin

Posted on Thu, 30 Sep 2021 16:25:53 -0400 by Courbois