Reputation: 648
Assume that we have a class with two overloaded main methods.
public class MyClass {
public static void main(int x){
System.out.println("(int x )version");
}
public static void main(String[] args){
System.out.println("(String[] args)");
}
}
So, how do we call the (int x) version while running the program?
Upvotes: 0
Views: 431
Reputation: 21975
MyClass.main(5)
is an example of how to call it.
If you automatically want the main(int)
method to be called, just call it from the main(String[])
. I assume you want java to treat the inputs like ints. Here is a way to do it
public static void main(int ... ints) {
// Process
}
public static void main (String[] args) {
main(Arrays.stream(args).mapToInt(Integer::parseInt).toArray());
}
Upvotes: 3
Reputation: 1795
Yes, you can overload main
No, you cannot start you program calling an overload, JVM will ever call the main signature main(String[] args) overload.
//JVM will call this method manually if you want
public static void main(String[] args,int a){
// some code
}
//JVM will call this method to start your program
public static void main(String[] args){
// some code
}
Upvotes: 0
Reputation: 285403
The only way is to call it is as you'd call any other static method, for example:
public static void main(String[] args) {
// call other "main"
main(5);
}
The issue is that the true main method's method signature must match precisely what the JVM is expecting for it to be. All other "main" method overloads are just simply like any other method to the JVM and will not be a location of automatic program startup.
Upvotes: 2