3

I am currently learning Kotlin and found the usage of a companion object along the way.

I would like to know why it was done this way?

vishnus
  • 728
  • 2
  • 7
  • 20
  • "this feature" meaning what? The content of the post should show what you are asking about, it shouldn't be a continuation of the title. – OneCricketeer Oct 16 '16 at 06:47
  • I'm still not sure I follow. Java has class objects as well. They are called instances, though. – OneCricketeer Oct 16 '16 at 07:07
  • @cricket_007 - In Kotlin, we cannot define static variables, there is no concept of it. The closest thing to static variables and something similar in syntax to statics are known as class objects, the members of which can be called using just the class name – vishnus Oct 16 '16 at 07:11
  • 1
    I'm only here for the Java tag. I can't answer Kotlin topics, but I think I found what you are referring to and Scala does the same thing. Static methods are just syntactic sugar saying you don't need an instance of the object. Kotlin discusses companion objects, but essentially you have singleton classes. Those are "class objects". You'd have to get a Kotlin developer in here to answer "why" – OneCricketeer Oct 16 '16 at 07:13
  • Please find an up-to-date reference for your learning of Kotlin. "Class objects" existed in pre-1.0 versions of Kotlin; the current version calls them "companion objects". – yole Oct 16 '16 at 07:34

0 Answers0