Dragonborn
Dragonborn

Reputation: 1815

Why does Kotlin lang allow only single main function in project?

Doesn't this take away the feature of having multiple main entry points in java which can be called as and when required.

Upvotes: 13

Views: 5515

Answers (2)

Sergey Mashkov
Sergey Mashkov

Reputation: 4760

UPDATE: recent versions of Kotlin allow multiple main functions even in the same package (if they are in different files).

You can have multiple main functions in your project but only one main function per package

The reason why you can't make multiple main functions in package is that all functions in package are stored in Package class so you can't have multiple functions in a class with same signatures.

So if you want multiple main functions you have to define them in different packages

Upvotes: 23

Andrey Breslav
Andrey Breslav

Reputation: 25767

In addition to Sergey Mashkov's comment: you can put a main inside an object and mark it @JvmStatic:

object Main {
    @JvmStatic 
    fun main(args: Array<String>) {
        println("Hello, world!")
    }
}

Upvotes: 24

Related Questions