Skip to main content
12 of 12
removing the extraneous "." in order for the code to actually compile

A. Old Java Way :

  1. Declare a companion object to enclose a static method / variable

     class Foo{ companion object { fun foo() = println("Foo") val bar ="bar" } } 
  2. Use :

     Foo.foo() // Outputs Foo println(Foo.bar) // Outputs bar 


B. New Kotlin way

  1. Declare directly on file without class on a .kt file.

     fun foo() = println("Foo") val bar ="bar" 
  2. Use the methods/variables with their names. (After importing them)

    Use :

     foo() // Outputs Foo println(bar) // Outputs bar 

erluxman
  • 19.6k
  • 22
  • 101
  • 129