Question

main accepts only an array of String (String args[]), and that from the console. How, then, do I pass other arguments that main needs? The situation is simply this: main calls methods that need input arguments which are objects. Could not find a way to do this. What am I missing? Interfaces maybe?

Was it helpful?

Solution 2

The main() method is the entry point to start a Java program. If you relay on input parameters you need to represent them as strings. Use those to instantiate java objects and pass those to the methods which require them. The strings passed to main() may represent a path to a properties file or even a path to a spring context which to start-up. You are not really restricted. Consider the main() method to bootstrap your application and the string parameter to point your application to arbitrary types of more complex information if needed.

OTHER TIPS

How could you pass objects from the command line? The command line is JUST a string (and that's what gets passed to main - an array of Strings).

You should construct your objects in main (and probably set some attributes based on command line parameters) and call the desired methods afterwards.

No. You can't send anything else apart from String[] to your main() method. main() is meant to be used that way.

You may use varargs instead of string[], like:

public static void main(String...args){

}

(String args[])....it means main method takes only Strings as arguments from command line....Incase u want some other datatype like int or something else as arguments...u can declare them in the main method as follows...

Int x=Integer.parseInt(args[0]);
Int y=Integer.parseInt(args[1]);
...

after declaring like this in main method...u can give integer values as arguments.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top