Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

from where does public static void main gets called?

Tags:

java

I was just wondering from where actually our main method gets called. As in eclipse when we run it as an application it automatically gets called. But if i write another method with same signature but different name it doesn't get called

like image 375
Shruti Rawat Avatar asked Jun 29 '13 12:06

Shruti Rawat


People also ask

What is public static void Main called?

The keyword public static void main is the means by which you create a main method within the Java application. It's the core method of the program and calls all others. It can't return values and accepts parameters for complex command-line processing.

How does public static void main work?

The main() method is static so that JVM can invoke it without instantiating the class. This also saves the unnecessary wastage of memory which would have been used by the object declared only for calling the main() method by the JVM.

Why public static void main is written?

public is used as an access modifier for a main method . static is used so that it can directly load in memory with creating any instance. void is used because it done not return any value and main is the entry point of program.

Is public static void main compulsory?

Need of static in main() method: Since main() method is the entry point of any Java application, hence making the main() method as static is mandatory due to following reasons: The static main() method makes it very clear for the JVM to call it for launching the Java Application.


1 Answers

From the documentation of the Java Virtual Machine:

DESCRIPTION

The java tool launches a Java application. It does this by starting a Java runtime environment, loading a specified class, and invoking that class's main method. The method declaration must look like the following:

public static void main(String args[])

The method must be declared public and static, it must not return any value, and it must accept a String array as a parameter. By default, the first non-option argument is the name of the class to be invoked. A fully-qualified class name should be used. If the -jar option is specified, the first non-option argument is the name of a JAR archive containing class and resource files for the application, with the startup class indicated by the Main-Class manifest header.

The Java runtime searches for the startup class, and other classes used, in three sets of locations: the bootstrap class path, the installed extensions, and the user class path.

Non-option arguments after the class name or JAR file name are passed to the main function.

The javaw command is identical to java, except that with javaw there is no associated console window. Use javaw when you don't want a command prompt window to appear. The javaw launcher will, however, display a dialog box with error information if a launch fails for some reason.

like image 135
Konstantin Yovkov Avatar answered Sep 28 '22 02:09

Konstantin Yovkov