I\'m just beginning to write programs in Java. What does the following Java code mean?
public static void main(String[] args)
What
Even tho OP is only talking about the String[] args
, i want to give a complete example of the public static void main(String[] args)
.
Public
: is an Access Modifier, which defines who can access this Method. Public means that this Method will be accessible by any Class(If other Classes are able to access this Class.).
Static
: is a keyword which identifies the class related thing. This means the given Method or variable is not instance related but Class related. It can be accessed without creating the instance of a Class.
Void
: is used to define the Return Type of the Method. It defines what the method can return. Void means the Method will not return any value.
main
: is the name of the Method. This Method name is searched by JVM as a starting point for an application with a particular signature only.
String[] args
: is the parameter to the main Method.
If you look into JDK source code (jdk-src\j2se\src\share\bin\java.c):
/* Get the application's main method */
mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
"([Ljava/lang/String;)V");
...
{ /* Make sure the main method is public */
...
mods = (*env)->CallIntMethod(env, obj, mid);
if ((mods & 1) == 0) { /* if (!Modifier.isPublic(mods)) ... */
message = "Main method not public.";
messageDest = JNI_TRUE;
goto leave;
...
You can see that the starting method in java must be named main
and must have the specific signature public static void main(String[] args)
The code also tells us that the public static void main(String[] args)
is not fixed, if you change the code in (jdk-src\j2se\src\share\bin\java.c) to another signature, it will work but changing this will give you other possible problems because of the java specs
Offtopic: It's been 7 years since OP asked this question, my guess is that OP can answer his own question by now.