Wrapped in a Class

The previous chapter mentioned that Kotlin is a class-based object-oriented language. So, if we wanted, we could wrap this code in a class and use that.

class HelloWorld {
  fun speak() {
    println("hello, world!")
  }
}

fun main() {
  HelloWorld().speak()
}

If you run this, you get the same output as the simple statement. We just wrapped it in a class to show off Kotlin classes a bit.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.