Skip to content

Instantly share code, notes, and snippets.

@ajoz
Created June 5, 2017 09:19
Show Gist options
  • Save ajoz/015e267881b563012af00b6c24633ac1 to your computer and use it in GitHub Desktop.
Save ajoz/015e267881b563012af00b6c24633ac1 to your computer and use it in GitHub Desktop.
public static void main function in Kotlin
class Foo {
// This will not work because its not a static method!
// You cannot annotate it with @JvmStatic (usable only
// in objects and companion objects)
// You have 4 options:
fun main(args: Array<String>) {
prinltn("This won't work as expected")
}
// this will clash with main above during compilation!
companion object {
@JvmStatic
fun main(args: Array<String>) {
println("Option 1: companion object")
}
}
object Test {
@JvmStatic
fun main(args: Array<String>) {
println("Option 2: internal object (inner static final class)")
}
}
}
object Test2 {
@JvmStatic
fun main(args: Array<String>) {
println("Option 3: just an object")
}
}
fun main(args: Array<String>) {
println("Option 4: free floating function")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment