Kotlin Getting started with Kotlin Hello World using an Object Declaration


Example

You can alternatively use an Object Declaration that contains the main function for a Kotlin program.

package my.program

object App {
    @JvmStatic fun main(args: Array<String>) {
        println("Hello World")
    }
}

The class name that you will run is the name of your object, in this case is my.program.App.

The advantage to this method over a top-level function is that the class name to run is more self-evident, and any other functions you add are scoped into the class App. You then also have a singleton instance of App to store state and do other work.

See also: