Shruti Rawat
Shruti Rawat

Reputation: 697

from where does public static void main gets called?

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

Upvotes: 6

Views: 1257

Answers (6)

when you execute a java class step 1 : class file is loaded in to JVM

step 2 : JVM read the bytecode

step 3 : JVM searches all the keywords

step 4 : JVM search for static blocks

step 5 : JVM searches for asynchronous blocks

step 6 : JVM creates a HASH MAP with key being the priorities

step 7 : according to priority JVM executes static blocks from top to bottom , asysnchronous blocks from top to bottom

step 8 : after this JVM searches for the method with the declaration

public static void main (String args[])

and executes it

so to answer your question , when a class is being executed JVM calls the main method

Upvotes: 0

tkroman
tkroman

Reputation: 4798

For *.jar-files you set main class (i.e. class, whose main() is to be called) by providing Main-Class: classname in manifest. In command line invocation you just do java Main_class_name. You can set these up in your IDE of choice (in IntelliJ Idea it's, for example, Main class entry in 'Edit configurations' in Run menu. In Eclipse or NetBeans, I guess, something similar.

Upvotes: 0

stinepike
stinepike

Reputation: 54682

main method the entry point for the program. It is called once by the JVM when the program starts.

Upvotes: 0

morgano
morgano

Reputation: 17422

It is specified in The java Language Specification, 12.1:

"The Java Virtual Machine starts execution by invoking the method main of some specified class, passing it a single argument, which is an array of strings"

Upvotes: 4

NPE
NPE

Reputation: 500367

It is invoked automatically by the JVM, as specified by the JVMS (§5.2. Java Virtual Machine Startup):

The Java Virtual Machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (§5.3.1). The Java Virtual Machine then links the initial class, initializes it, and invokes the public class method void main(String[]).

Thus, main() is special. A different method with the same signature but different name won't automatically get called by the JVM.

Upvotes: 1

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

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.

Upvotes: 7

Related Questions