Is the "main" function must be a standalone function?

I'm new to Kotlin, and found the "main" entry function in Kotlin is a standalone function:

  fun main(args:Array<String>) {
     println(“hello”)
   }

But in Java, the “main” method should be inside a class.

I wonder if the “main” function must be a standalone function? Can it will inside some classes like java?

2 Likes

You can't put main() inside a class in Kotlin. In Java it must be static, and the only way to make a function static in Kotlin is to put it directly under a package.

1 Like

You can’t put main() inside a class in Kotlin.
In Java it must be static, and the only way to make a function static in Kotlin is to put it directly under a package.

Not exactly.

You CAN put a main() fun inside a class, and make it a static method of JVM, by using the @JvmStatic annotation.

See:
http://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#static-methods

4 Likes

Additionally main() must be defined inside a companion object for you to have access to @JvmStatic

2 Likes