What is an equivalent of private static final field in Kotlin?

时间:2017-08-05 11:39:30

标签: java android kotlin

Actually the question is, which approach is better and which convention do you use?

E.g. in order to improve readability we've added this field in Java class:

class Game {

    private static final int MAX_PLAYERS = 10;

    public boolean canWePlay(int playersCount) {
         return playersCount <= MAX_PLAYERS;
    } 

    // getters and setters
}

Now the better approach is to use companion object:

class Game {

    fun canWePlay(playersCount: Int): Boolean {
        return playersCount < MAX_PLAYERS
    }

    companion object {

        val MAX_PLAYERS = 10
    }
}

or and const val outside the class:

private const val MAX_PLAYERS = 10

class Game {

    fun canWePlay(playersCount: Int): Boolean {
        return playersCount < MAX_PLAYERS
    }
}

or maybe do you use different approaches?

And the additional question is, if we use the second convention, should we use camel case or uppercase with underscore? (I'm talking manly about Android code).

0 个答案:

没有答案