Kotlin uses static variables and static methods for detailed explanation

  • 2021-10-25 08:01:40
  • OfStack

Preface

In the daily development process, static variables and static methods are our common usage. I believe everyone is no stranger in Java, so how to use them in Kotlin?

In fact, it is very simple, only one variable and method is included in the companion object domain, such as this:


class Constant {
 companion object {
  //  Interface root address 
  const val BASE_URL = "http://xxxx.xxx.xxx/"
  //  Friendship League 
  const val UMENG_APP_KEY = "xxxxxxxxxx"
  const val UMENG_CHANNEL = "umeng"
  //  Microblogging 
  const val WEIBO_APP_KEY = "xxxxxxxx"
  const val WEIBO_SECRET = "xxxxxxxxxx"
  
  
  fun getVideoFactor(){
   // do some work
  }
 }

}

Is it very simple after reading it? You can use this directly in pure kotlin code:


// Initialize the APIKey
  PlatformConfig.setWeixin(Constant.WECHAT_APP_ID, Constant.WECHAT_APP_SECRET)
  PlatformConfig.setSinaWeibo(Constant.WEIBO_APP_KEY, Constant.WEIBO_SECRET, Constant.WEIBO_AUTH_RETURN_URL)

However, if we are using a mixture of Java and kotlin, we cannot use static variables or methods in Java code in the way of Constant. Static variables, but in the following way:


// Initialize the APIKey
  PlatformConfig.setWeixin(Constant.Companion.WECHAT_APP_ID, Constant.WECHAT_APP_SECRET)
  PlatformConfig.setSinaWeibo(Constant.Companion.WEIBO_APP_KEY, Constant.WEIBO_SECRET, Constant.WEIBO_AUTH_RETURN_URL)

What if we want to use it directly by class name and static variable as kotlin does? We can annotate static variables and static methods with the annotations @ JvmField and @ JvmStatic, respectively, and then I can use static members directly in Java code as before!

For example:


/**
 * @author moosphon on 2018/12/12
 * desc:  Anomalous system 1 Processor 
 */
class ExceptionHandler {


 companion object {
  @JvmField
  var errorCode = NetRequestStatus.UNKNOWN_ERROR

  @JvmField
  var errorMessage = " The request failed, please try again later "

  @JvmStatic
  fun handleException(e : Throwable): String{
   e.printStackTrace()
   when(e){
    is SocketException -> {
     Logger.e("ExceptionHandler", " Network connection exception:  " + e.message)
     errorCode = NetRequestStatus.NETWORK_ERROR
     errorMessage = " Network connection exception "
    }

    is JsonParseException -> {
     Logger.e("ExceptionHandler", " Data parsing exception:  " + e.message)
     errorCode = NetRequestStatus.PARSE_ERROR
     errorMessage = " Data parsing exception "
    }

    else -> {
     try {
      Logger.e("ExceptionHandler", " Other errors:  " + e.message)
     } catch (e1: Exception) {
      Logger.e("ExceptionHandler", " Unknown error:  " + e.message)
     }

     errorCode = NetRequestStatus.UNKNOWN_ERROR
     errorMessage = " Unknown error, 1 Get up and pray to get better soon ~ "
    }
   }
   return errorMessage
  }
 }
}

Some time ago, I was busy, and I will continue to bring you articles on kotlin. We will wait and see.

Summarize


Related articles: