kotlin-Detailed explanation of several ways to achieve static

https://www.jianshu.com/p/a5b0da8bbba3
compile kotlin into java

First, let’s take a digression, how do we look at the java code corresponding to kotlin? Kotlin will eventually be compiled into a java class to run on the JVM. Sometimes we really want to see what the code written in kotlin looks like after being compiled. Help us understand Kotlin syntax

It’s actually very simple. AS tools are provided

  1. tools -> Show kotlin Bytecode

     

     

  2. Click Decompile

     

     

ok, that's it


kotlin implements a static way

In the kotin language, there is actually no concept of java static. Basically, a static object is used to simulate the static properties and methods of the class. There are currently 4 ways to implement it:

  • companion object -companion object, the way to declare a singleton
  • @JvmField + @JvmStatic annotation -use the annotation tag to declare the static part
  • object singleton -static singleton is actually similar to companion object
  • const -Uniqueness in the package, free from the constraints of classes, characteristics of kotlin, compiled in java to generate a kotlin.kt file to provide support for alignment

companion object

Companion object companion object This is the most commonly used method in Kotlin. In companion object, we can declare attributes and methods. The calling method in Kotlin feels the same as static in java. For example, see the following:

 

class BookKotlin {

    var name: String = "AA"

    fun speak() {}

    companion object instance {

        var nameStatic: String = "BB"

        fun speakStatic() {}

    }
}

Used in kotlin as follows:

 

Call static methods and properties in kotlin

 

 

Call member methods and properties in kotlin

Looking at the picture above, when we use it in kotlin, it feels like static in java. The static parameter method and the member parameter method can be effectively distinguished. Let’s take a look at the use in java.

Used in java as follows:

 

Class name. There are no static properties and methods, only one static object

 

Under this static object, we can find the static properties and methods we declared

 

new an object we can use member variables and methods

The use of kotlin is ok, but everything changes when it comes to java. Why, because kotlin will eventually be compiled into java files and run on JVM, kotlin does not provide its own unique VM, so kotlin code still needs to be converted into java code Yes, or how does kotlin advertise that kotlin and java can be seamlessly called

All member variables in kotlin, whether static or not, have get/set methods. In addition, kotlin does not declare the static parameters and methods we declare as static, but is used in transit through the static object instance, which allows We are puzzled. What happens after kotlin becomes java? Will it be different from our original intention and affect our use?

The name of the static object instance is the name followed by the companion object. You can leave it alone. If you don’t write it, the default is INSTANCE (different versions will change)

Let's take a look at it by converting kotlin into java code

Convert the kotlin code to java:

 

public final class BookKotlin {

   private static String nameStatic = "BB";

   public static final BookKotlin.instance instance = new BookKotlin.instance((DefaultConstructorMarker)null);

   private String name = "AA";

   public final String getName() {
      return this.name;
   }

   public final void setName(@NotNull String var1) {
      Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
      this.name = var1;
   }

   public final void speak() {
   }

   public static final class instance {
      @NotNull
      public final String getNameStatic() {
         return BookKotlin.nameStatic;
      }

      public final void setNameStatic(@NotNull String var1) {
         Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
         BookKotlin.nameStatic = var1;
      }

      public final void speakStatic() {
      }

      private instance() {
      }

      // $FF: synthetic method
      public instance(DefaultConstructorMarker $constructor_marker) {
         this();
      }
   }
}

After seeing the java code, everyone is completely clear. The companion object is really not for nothing, it is really an object. Add a static internal class implementation object of static final class inside the class to simulate static characteristics


@JvmField + @JvmStatic annotation

We have seen the companion object of the companion object above, so is Kotlin really unable to implement static? No, Kotlin still provides related methods. This is the @JvmField + @JvmStatic annotation, which means that the members and methods are declared using the JVM provided characteristic

We modify the above code:

 

class BookKotlin {

    companion object {

        @JvmField
        var nameStatic: String = "BB"

        @JvmStatic
        fun speakStatic() {
        }
    }

    var name: String = "AA"

    fun speak() {}
}

Used in kotlin as follows:

 

 

Used in java as follows:

 

 

 

Convert the kotlin code to java:

 

public final class BookKotlin {

   @JvmField
   public static String nameStatic = "BB";

   @JvmStatic
   public static final void speakStatic() {
      Companion.speakStatic();
   }

   public static final BookKotlin.Companion Companion = new BookKotlin.Companion((DefaultConstructorMarker)null);

   private String name = "AA";

   @NotNull
   public final String getName() {
      return this.name;
   }

   public final void setName(@NotNull String var1) {
      Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
      this.name = var1;
   }

   public final void speak() {
   }


   public static final class Companion {
      @JvmStatic
      public final void speakStatic() {
      }

      private Companion() {
      }

      // $FF: synthetic method
      public Companion(DefaultConstructorMarker $constructor_marker) {
         this();
      }
   }
}


object singleton

Kotlin itself provides a singleton implementation: object, which is directly used to modify the class. A class modified with object cannot be a new object, but can only use the singleton provided by object

 

object BookKotlin {

    var name: String = "AA"

    fun speak() {}
}

Used in kotlin as follows:

 

 

 

Object modified classes can no longer create objects

Used in java as follows:

 

 

 

Convert the kotlin code to java:

 

public final class BookKotlin {

   private static String name;
   public static final BookKotlin INSTANCE;

   public final String getName() {
      return name;
   }

   public final void setName(@NotNull String var1) {
      Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
      name = var1;
   }

   public final void speak() {
   }

   static {
      BookKotlin var0 = new BookKotlin();
      INSTANCE = var0;
      name = "AA";
   }
}

Although the name is static, it is still private, and the call must be transferred through a static singleton. It is really a singleton


const

const is written outside the class, the effect = @JvmField, but it cannot modify the method, nor can it be mixed with @JvmField. It is generally used to declare commonly used values. It is not very useful and can only use val. Var is not good. If you want to modify the method , Just do not write const

 

const val name: String = "AA"
fun adk(){}
class BookKotlin {

    fun speak() {}
}

It is used in kotlin as follows:
kotlin does not have the concept of a package, so it is used directly. Note that this is actually equivalent to leaving the scope of the class, and the name cannot be duplicated within the scope of a package

 

 

 

Used in java as follows:

 


This BookKotlinKt file is an auxiliary class generated specifically to support this Kotlin feature. In Java, we use this class to call the content of the const declaration. Why do we need this class, because Java has the concept of a package, all The file must have a unified address specification, so the kotlin level can only provide XXXKt files to support it alone, in fact, this is out of the scope of java

 

Convert the kotlin code to java:

 

public final class BookKotlin {
   public final void speak() {
   }
}

public final class BookKotlinKt {
   @NotNull
   public static final String name = "AA";

   public static final void adk() {
   }
}

In the java code, it exists as a Kotlin auxiliary class, so there are really very few people who use it, which is too restrictive



Author: turtle forward the
link: https: //www.jianshu.com/p/a5b0da8bbba3
Source: Jane books
are copyrighted by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/az44yao/article/details/113111115