No class -- just "fun main"?


Why isn’t there a class declared in the example hello world?

that really threw me for a loop. Ok, there’s no class declared. I don’t have a library, but let’s say I have kotlin library. I can build that library as a JAR? Not sure how to add the JAR to the classpath, but let’s leave that for gradle. Now the library is a compile time dependency.

The Kotlin app still runs without a class being declared in the “main” entry point? (Not planning on using Android, just Kotlin for the JVM.)


The compiler will create a class and the free functions will be static methods of that class.

fun main(args: Array<String>) {}

in a file called Main.kt will compile to something like:

public class MainKt {
    public static void main(String[] args) {}


I see.

Is it typical or atypical to explicitly declare a class Main (or
similar)? How does Kotlin know the entry point for a JAR?


Unless you use a @JvmStatic annotation you cannot create a static main function as required as a member of a Main class. The idiomatic way would be a free-standing function (outside of a class). The class name is just the filename with Kt appended (and extension removed). You can use an annotation to change that name though. For where to find the main function, you will have to put the right class name in your manifest the same way as in Java - it is how Java determines the main function to use.