Kotlin logo

The Kotlin Blog

Kotlin Programming Language by JetBrains

Language design

“Static constants” in Kotlin

Kotlin is designed so that there’s no such thing as a “static member” in a class. If you have a function in a class, it can only be called on instances of this class. If you need something that is not attached to an instance of any class, you define it in a package, outside any class (we call it package-level functions):

package foo

fun bar() {}

But sometimes you need static constants in your class: for example, to comply with requirements of some framework or to use serialization. How do you do this in Kotlin? There are two things in Kotlin that resemble Java’s statics: aforementioned package-level functions and class objects. I’ll explain briefly what class objects are and then proceed to static constants.

Class Objects

A class (not inner and not local) or trait may declare at most one class object associated with it. For example:

class Foo {
  class object {
    val bar = 1

  val baz = 2

Here we have a class, Foo, that declares a class object, which in turn has a member property bar. This means that we can call bar directly on a class name (just like in Java):


Note that we can not call bar on an instance of Foo:

val foo = Foo()
println(foo.bar) // compilation error

That’s because bar is not a member of Foo, only of its class object. Class object is a separate entity associated with the class, and does not share members with its instances. Neither can we call baz on the class name:

prinltn(Foo.baz) // error

This is because baz is a member of Foo, not of its class object, so you can only call baz on instances of Foo.

Now, let’s look at how class objects work. First, there’s a separate JVM class generated for a class object, and bar is a member of that class. If you access class objects from Java, you have to say something like this:

/* Java */

Class object is an instance stored in a static field inside the class it is defined in, and its properties are accessed with getters/setters.

Static Constants

A property in a class object works as well as a static field (and even better), but only in Kotlin. As we saw above, for Java it looks different, and this may cause problems if some framework or convention (in Java) requires you to have a real static field.

Disclaimer: in Kotlin M5.3 there’s no way to have a static field in your class. It was implemented very recently, so you have it only in the latest nightly build.

When you define a public or internal property in your class object, and do not specify custom getter, nor setter, Kotlin automatically stores it directly in the enclosing class, so that in Java you can say


There’s no difference for your Kotlin code: everything works as it should. Even in Java the old-fashioned Foo.object$.getBar(), but now you can also have real static constants in your class.


Comments below can no longer be edited.

7 Responses to “Static constants” in Kotlin

  1. Peter Niederwieser says:

    July 14, 2013

    What about static methods? There are quite a few Java frameworks that require declaration of static methods. JUnit is one example (@BeforeClass/@AfterClass method), but there are others.

    • Andrey Breslav says:

      July 15, 2013

      Static methods are harder. Looks like we’ll need a workaround test runner to use class-level initializations in JUnit 4 in Kotlin

      • Peter Niederwieser says:

        July 15, 2013

        I’m not worried about JUnit (or Play Framework) in particular. I just wanted to point out that for a pragmatic industrial-strength language designed with excellent Java compatibility in mind, not being able to declare static methods seems like a rather big omission. That said, I don’t know what’s holding you back. Keep up the good work!

  2. Paul says:

    July 23, 2013

    What about Serializable? It forces us to have static final serialversionuid. How can we implement it in Kotlin?

    • Andrey Breslav says:

      July 23, 2013

      As said above:

      class Foo {
      class object {
      val serialVersionUID: Long = 239

  3. Travis Romney says:

    October 7, 2013

    Is it me or does this seem like to much ceremony just to create a constant?

    • Andrey Breslav says:

      October 7, 2013

      You can create a constant as simple as this:

      package foo

      val CONST = 1

      It’s a constant that belongs to a class that takes some extra syntax