6
public static void main(String[] args) { boolean t=true; System.out.println("Before return"); if(t) return; System.out.println("not execute"); } 

In the above code when the return is used then it should return to the function which calls the main function. Who exactly calls the main function?

1
  • 1
    You can call one main method from another as well as have the launcher call it. Commented Oct 19, 2010 at 19:05

8 Answers 8

18

The Java Virtual Machine.

Sign up to request clarification or add additional context in comments.

Comments

10

Java classes are executed within a larger context (a particular JVM as others have noted). Below are some possibilities:

In all cases the main() method is the canonical entry point to executing code given a particular class. From the docs on the java JVM:

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.

You state:

In the above code when the return is used then it should return to the function which calls the main function.

There may not be any other Java function (in fact there usually isn't) which calls the main() function. It's the convention for declaring a well-known entry point. If the JVM is launched to run your class's main() method, then when main() returns, the JVM exits, except in a few special cases, e.g. there are other non-daemon threads running or there is a shutdown hook.

1 Comment

Your quote of my statement is missing my qualifying clause: "except in a few special cases, e.g. there are other non-daemon threads running or there is a shutdown hook." which covers your concerns.
2

Here's a nice example of invoking main() via JNI_CreateJavaVM.

Comments

0

See Running Java programs...

Comments

0

The JVM uses the main() as the starting point for a program just like the int main() in C++.

Comments

0

Agree with above statements as JVM calls the main method because it is the entry point to any class that has to be loaded in order to execute the class.

Comments

0

Jvm starts main thread to call main method.

Comments

0

Java main() method is invoked by : JVM (JAVA VIRTUAL MACHINE)

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.