What is special about main()? Are there other special functions?
To start a Java program you need
- a class file,
static void main(String[])
method in that class file.
So from outside of the package you'd be able to start any of these main
methods.
However if you try to call the main
method from another Kotlin file inside of the package, you'd get an error, because Kotlin can't disambiguate one method from the other.
You can call any of them from Java as you please because they're compiled in different class files (see further).
Can I create two static myfun() functions in same package?
You can't define two top-level methods with the same name in the same package in Kotlin (with the above exception).
This is what your code compiles to:
public final class Test1Kt {
public static final void main(@NotNull String[] args) { /* ... */ }
public static final void myFun(@NotNull String[] args) { /* ... */ }
}
public final class Test2Kt {
public static final void main(@NotNull String[] args) { /* ... */ }
public static final void myFun(@NotNull String[] args) { /* ... */ }
}
As far as JVM is concerned all of these methods could coexist in peace. But this is an implementation detail of Kotlin.
Let's forget for a second that Kotlin apps run on JVM. Pretend your only tool is Kotlin, and you can't use Java, perhaps you're writing a Kotlin cross-platform module. How could you have two top-level functions with the same name? How would you pick which one to call? Again, you'd get an error, because Kotlin couldn't disambiguate one method from the other.
Edit: As noted by @Todd this behavior has been even more strict in the past: Why does Kotlin lang allow only single main function in project?