In Java
, especially in Android studio
, every time that I want to run or test some Java
source code quickly, I will create public static void main
(shortkey: psvm + tab
) and the IDE will show "Play" button to run it immediately.
Do we have some kind of psvm
in Kotlin
- an entry point or something in order to run or test anything that quickly? Did try with this function but it wasn't working. (Even try with @JvmStatic
). Can we config somewhere in Android studio
?
fun main(args: Array<String>) {
}
The keyword public static void main is the means by which you create a main method within the Java application. It's the core method of the program and calls all others. It can't return values and accepts parameters for complex command-line processing.
Yes, we can change the order of public static void main() to static public void main() in Java, the compiler doesn't throw any compile-time or runtime error. In Java, we can declare access modifiers in any order, the method name comes last, the return type comes second to last and then after it's our choice.
Android Dependency Injection using Dagger with Kotlin In Java, once a method is declared as "static", it can be used in different classes without creating an object. With static methods, we don't have to create the same boilerplate code for each and every class.
public static void main(String[] args) Java main method is the entry point of any java program. Its syntax is always public static void main(String[] args) . You can only change the name of String array argument, for example you can change args to myStringArgs .
Put it inside a companion object with the @JvmStatic
annotation:
class Test {
companion object {
@JvmStatic
fun main(args: Array<String>) {}
}
}
You can just put the main function outside of any class.
In anyFile.kt do:
package foo
fun main(args: Array<String>) {
}
Either main + tab
or psvm + tab
work if you have your cursor outside of a class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With