Static Class in Kotlin

Earlier this week I need to create a static class in Kotlin. If you are coming from Java like myself, the syntax is slightly different, but still easy. Here’s the code

class Constants { 
  companion object {
    val constantKey = "KeyValue"    
    fun getConstant() : String { return "KeyValueFromFun" }
  } 
}

You can then use the class like this:

Constants.constantKey
Constants.getConstant()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s