Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

public static void main(String arg[ ] ) in java is it fixed?

Tags:

java

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?

like image 273
amar Avatar asked Apr 05 '12 12:04

amar


People also ask

Is public static void main String args valid?

public strictfp static void main(String[] args) { }synchronized and final are also valid keywords for the main method but they won't have an effect here.

What is public static void main String args in Java?

public static void main(String[] args) Java main method is the entry point of any java program. Its syntax is always public static void main(String[] args) . You can only change the name of String array argument, for example you can change args to myStringArgs .

Can we write static public void main String args in Java?

Yes, we can change the order of public static void main() to static public void main() in Java, the compiler doesn't throw any compile-time or runtime error. In Java, we can declare access modifiers in any order, the method name comes last, the return type comes second to last and then after it's our choice.

Why we use public static void main String args?

public static void main (string args[]) Explanation. In Java, JVM (Java Virtual Machine) will always look for a specific method signature to start running an application, and that would be the public static void main (String args[]).


1 Answers

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.

  • it must be public otherwise it would not be possible to call it
  • it must be static since you have no way to instantiate an object before calling it
  • the list of String 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)
  • the return type is 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

like image 135
Matteo Avatar answered Oct 20 '22 12:10

Matteo