简体   繁体   中英

Conversion of Java Static to Kotlin

How Do I Perform this Type of conversion in Kotlin:

 public class DummyStatic {

    static String Aloha;

    static {

        if(true) {
          Aloha = "Hello";
        } else {
            Aloha= "Bye";
        }
    }
}

I tried to complete this with Companion object and Object, but not able to achieve the required concept.

Statics are represented in companion objects in Kotlin. The static initalizer logic goes into the init{} of the companion object

class DummyStatic {

companion object
    var Aloha: String

    init {
        if(true) {
          Aloha = "Hello";
        } else {
            Aloha= "Bye";
        }
    }
}

I don't know if it will help you, but try to convert with:) https://try.kotlinlang.org/#/Kotlin%20Koans/Introduction/Java%20to%20Kotlin%20conversion/Task.kt

object DummyStatic {
  internal var Aloha:String
  init{
    if (true)
    {
      Aloha = "Hello"
    }
    else
    {
      Aloha = "Bye"
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM