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


#1

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

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.


#3

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