Reputation: 4345
I was recently asked in an exam if public static void main(String arg[])
format of main
method was fixed?
Can we change it?
Can we use main
without any of public
, static
or void
?
If not, why is it not hard coded that main(String arg[])
would stand for public static void main(String arg[])
always?
Upvotes: 16
Views: 53587
Reputation: 21
Public static void main(String [] ar)
To understand this we need to know the syntax of method and the array.
Syntax of method is :
return type methodName
so the main method is written along with void which is return type.
Syntax of Array:
datatype [] arrayName
the square braces indicates whether it is the of dimension array .Since we have one pair of square braces it is one dimension array.
The meaning of words in the main method:
Public: Public is the access specifier it is intended for the purpose of the JVM to execute the main method from any location.
Static : Static is a modifier.The main method must be declared as static so that the JVM can access the main method directly by using the class name.
When we execute a java program we use the class name so when we write static it will help the JVM to access the main method.
If we remove static then it becomes instance method,to access an instance method we should create and object and then invoke the method using the object reference.
void : Void is the return type of main method. The Caller of the main method is JVM and the JVM does not expect any value from the main method and there fore the main method should not return any value.This is the reason for specifying Void.
main : Main is the method name and it is fixed as per the Java coding conventions.
String[]: It is used for storing the command line arguments.The name of the array can be any valid Java identifier.
So after String[] we can give name as any valid java identifier it can be 'ar' or it can be 'args'.
Upvotes: 0
Reputation: 652
Public is a kind of access specifier due to which we can access it from outside the class. Since main is the function that acts as an execution point. Main function is called by the JVM which is outside the class so it must be declared as public.
Static is a kind of keyword used in java to specify that there is no need to create any instance of it. As we know main method also reside inside a class and to access the particular function of a class from outside the class (In this case from JVM), an instance of that particular class is needed, so to avoid these we simply put static to make access main method outside of class.
Void is the return type since other return type in main method is meaningless.
String is a pre-defined class name in java. And args [] is a variable of array types. It’s a variable name of String type object. So we can also change the name of args []. String class and its object can be passed in a running program as an argument to pass information to the main method from command line.
Upvotes: 2
Reputation: 26185
If not, why is it not hard coded that main(String arg[]) would stand for public static void main(String arg[]) always?
You can have methods called "main" with any signature and access you like. The special rules only apply to the method you want the JVM to call to start a program.
public class Test {
public static void main(String[] args) {
StrangeMain m = new StrangeMain();
System.out.println(m.main());
System.out.println(m.main(new String[]{"aaa","bbb"}));
}
}
class StrangeMain{
protected String main(){
return "xyzzy";
}
protected String main(String[] arg){
return arg[0];
}
}
compiles, runs, and outputs:
xyzzy
aaa
Upvotes: 0
Reputation: 14930
The signature of the main method is specified in the Java Language Specifications section 12.1.4 and clearly states:
The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String.
public
otherwise it would not be possible to call itstatic
since you have no way to instantiate an object before calling itString
arguments is there to allow to pass parameters when executing a Java program from the command line. It would have been possible to define it without arguments but is more practical like that (and similar to other languages)void
since it does not make sense to have anything else: a Java program can terminate before reaching the end of the main method (e.g., by calling System.exit()
)The method signature can therefore be:
public static void main( String[] args )
public static void main( String... args )
note that the varargs version (...
) is only valid from Java 5
As the Java language allows the brackets []
to be positioned after the type or the variable (the first is generally preferred),
public static void main( String args[] ) // valid but usually non recommended
is also valid
Upvotes: 26
Reputation: 1
public-main() method must be used by any one of the outside the class as well as inside the class so its public
static-static is necessary bcoz in java if we define class than we define object for that class and than and only than we can use that class..but instead of this we directly use by write the word static
void-for main() cant return any value like int or char main()-main is the function or method which we can use for accessing the future of java String-in java all we write consider as a string args-arguments
Upvotes: -2
Reputation: 40333
You can change it if you create a new loader for your app. The public static void main( String args[] ) format is just the default solution people working on the JVM found to call your Java programs, so that there is a definite way to do it.
The real implementation we have today just uses the JNI interface to call the public static void main (String args[]) method by using this function, so you could easily write exactly the same code if you wanted using JNI and have a different method to load your app.
Here's an example in code that was taken from this page.
Here's the current linux launcher program, the method lookup starts here.
Upvotes: -2
Reputation: 28951
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;
...
It becomes very clear that it must have only this signature.
Upvotes: 2
Reputation: 94469
I cannot answer for the arguments of the method but it must be public because the jvm must be able to access the function and it must be static because the jvm does not know how to create an instance of your class.
This post provides a good detailed answer about the reasoning for static: Why is the Java main method static?
This post provides a good answer for why main is void: Why is main() in java void?
Upvotes: 1
Reputation: 12766
The main method must be public
so it can be found by the JVM when the class is loaded. Similarly, it must be static
so that it can be called after loading the class, without having to create an instance of it. All methods must have a return type, which in this case is void.
Upvotes: 0