+ 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.
5 Réponses
+ 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.
+ 4
It's funny how the top answer doesn't have anything to do with the question and still it's the top answer...
+ 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. :-) :-) :-)
+ 2
In the cmd
When you execute
javac programName param1 param2