+ 5

How can I pass value to main method

As u know, Java's main method starts with static void main(String[] args) And here, I do want to know how can I pass the value to the main method.

1st Jan 2017, 12:09 AM
Sai Saing Hmine Tun
Sai Saing Hmine Tun - avatar
5 Answers
+ 8
In the command line, you write: java ProgramName value1 value2 and then, when the program runs, the main method creates an array of Strings for you to use later iin your program. If you don't pass any parameter, the array will be empty. That's why you write: public static void main( String[] args )... example: if you run the program by typing: java NameList John Robert Donna args[0] = "John" args[1] = "Robert" args[2] = "Donna" keep in mind that you don't pass the parameters when you compile the program with javac command but only when you run it with java command. Hope I helped.
1st Jan 2017, 4:12 AM
Shahar Levy
Shahar Levy - avatar
+ 4
It's funny how the top answer doesn't have anything to do with the question and still it's the top answer...
3rd Mar 2017, 2:13 AM
Shahar Levy
Shahar Levy - avatar
+ 3
ok, thz guys. I've got the answer 1. I creat a java file using notepad. 2. I save it as .java file. 3. I compile it with Java Compiler using javac HelloWorld.java command 4. I then run this class file using java HelloWorld Hello command At last, it works, I print the value of this using for loop. :-) :-) :-)
4th Jan 2017, 2:21 PM
Sai Saing Hmine Tun
Sai Saing Hmine Tun - avatar
+ 2
In the cmd When you execute javac programName param1 param2
1st Jan 2017, 12:49 AM
Dago VF
Dago VF - avatar