For example, if I want to print an array length, I can not do that:
public class Test{
public static void main(String [] args){
System.out.println(worngParam({"first", "second", "ect"}));
}
public static int worngParam(String [] strings){
return strings.length;
}
}
This is an error!
The first two lines in main must be
String [] strings = {"first", "second", "ect"};
System.out.println(worngParam(strings));
But even so I can do that:
System.out.println(args.length);//If of course args is not empty
My question is how does the parameter get into the main method?
Although any method can accept a constant variabls such as 3, "word", 'a'
. But she can not get an initialization of an array like this {1,8}
or {"word2", "word3"}
Supposed you run your java class file like this
javac Test.java
java -cp . Test firstParam secondParam thirdParam
Then in your main method args will have a value like
args = new String[]{"firstParam", "secondParam", "thirdParam"};