Using static variables and static methods in Kotlin

Keywords: Java network

In the daily development process, static variables and static methods are our common usage. I believe that we are not unfamiliar with Java, so how to use them in Kotlin? In fact, it's very simple to include variables and methods in the company object domain. For example:

class Constant {
    companion object {
        //Interface root address
        const val BASE_URL = "http://xxxx.xxx.xxx/"
        //Friend League
        const val UMENG_APP_KEY = "xxxxxxxxxx"
        const val UMENG_CHANNEL = "umeng"
        //Micro-blog
        const val WEIBO_APP_KEY = "xxxxxxxx"
        const val WEIBO_SECRET  = "xxxxxxxxxx"


        fun getVideoFactor(){
            // do some work
        }
    }

}

Is it very simple after watching? You can use this directly in pure kotlin Code:

//Initialize APIKey of each platform
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 use Java and kotlin mixed development, we can't use static variables or methods through Constant. Static variables in Java code, but through the following ways:

//Initialize APIKey of each platform
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 through class name and static variables like kotlin? We can annotate static variables and static methods with the help of annotation @ JvmField and @ JvmStatic. Then I can directly use static members in Java code as before! For example:

/**
 * @author moosphon on 2018/12/12
 * desc: Unified handler of exceptions
 */
class ExceptionHandler {


    companion object {
        @JvmField
        var errorCode = NetRequestStatus.UNKNOWN_ERROR

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

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

                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, pray together and get better soon~"
                }
            }

            return errorMessage
        }

    }

}

Posted by theprofession on Fri, 25 Oct 2019 07:59:19 -0700