Constants in Kotlin — what's a recommended way to create them?

后端 未结 13 1161
不知归路
不知归路 2020-12-04 18:45

How is it recommended to create constants in Kotlin? And what\'s the naming convention? I\'ve not found that in the documentation.

companion object {
    //1         


        
13条回答
  •  再見小時候
    2020-12-04 19:26

    For primitives and Strings:

    /** The empty String. */
    const val EMPTY_STRING = ""
    

    For other cases:

    /** The empty array of Strings. */
    @JvmField val EMPTY_STRING_ARRAY = arrayOfNulls(0)
    

    Example:

    /*
     * Copyright 2018 Vorlonsoft LLC
     *
     * Licensed under The MIT License (MIT)
     */
    
    package com.vorlonsoft.android.rate
    
    import com.vorlonsoft.android.rate.Constants.Utils.Companion.UTILITY_CLASS_MESSAGE
    
    /**
     * Constants Class - the constants class of the AndroidRate library.
     *
     * @constructor Constants is a utility class and it can't be instantiated.
     * @since       1.1.8
     * @version     1.2.1
     * @author      Alexander Savin
     */
    internal class Constants private constructor() {
        /** Constants Class initializer block. */
        init {
            throw UnsupportedOperationException("Constants$UTILITY_CLASS_MESSAGE")
        }
    
        /**
         * Constants.Date Class - the date constants class of the AndroidRate library.
         *
         * @constructor Constants.Date is a utility class and it can't be instantiated.
         * @since       1.1.8
         * @version     1.2.1
         * @author      Alexander Savin
         */
        internal class Date private constructor() {
            /** Constants.Date Class initializer block. */
            init {
                throw UnsupportedOperationException("Constants.Date$UTILITY_CLASS_MESSAGE")
            }
    
            /** The singleton contains date constants. */
            companion object {
                /** The time unit representing one year in days. */
                const val YEAR_IN_DAYS = 365.toShort()
            }
        }
    
        /**
         * Constants.Utils Class - the utils constants class of the AndroidRate library.
         *
         * @constructor Constants.Utils is a utility class and it can't be instantiated.
         * @since       1.1.8
         * @version     1.2.1
         * @author      Alexander Savin
         */
        internal class Utils private constructor() {
            /** Constants.Utils Class initializer block. */
            init {
                throw UnsupportedOperationException("Constants.Utils$UTILITY_CLASS_MESSAGE")
            }
    
            /** The singleton contains utils constants. */
            companion object {
                /** The empty String. */
                const val EMPTY_STRING = ""
                /** The empty array of Strings. */
                @JvmField val EMPTY_STRING_ARRAY = arrayOfNulls(0)
                /** The part 2 of a utility class unsupported operation exception message. */
                const val UTILITY_CLASS_MESSAGE = " is a utility class and it can't be instantiated!"
            }
        }
    }
    

提交回复
热议问题