Reputation: 119
I know we can compile and run a java program successfully without a main() method, but why we still need main() method in java's main class?
Upvotes: 11
Views: 45893
Reputation: 17903
Quoting Java Language Specification (JLS)
A 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.
Now a typical definition of main method from which execution start is
public static void main(String[] args)
public
- accessible from anywhere
static
- accessible statically, meaning without an instance (as JVM starts, it has no instance of the class containing main method, hence static).
void
- returns void.
So calling the main()
method is 'hardcoded' in JVM as the starting point.
Upvotes: 7
Reputation: 77
.java
file).class
file .class
fileAfter that JVM takes the responsibility
b) After that, interpreter in JVM wants to read the code. In the bunch of code where to start reading is the question for JVM?
Answer: To solve this problem we are giving the main
keyword as a clue for the JVM to start execution in this method.
Upvotes: 2
Reputation: 26737
Every Java application must contain a main method whose signature looks like this:
public static void main(String[] args)
How the main Method Gets Called
The main method in the Java language is similar to the main function in C and C++. When the Java interpreter executes an application (by being invoked upon the application's controlling class), it starts by calling the class's main method. The main method then calls all the other methods required to run your application.
If you try to invoke the Java interpreter on a class that does not have a main method, the interpreter refuses to compile your program and displays an error message similar to this:
In class NoMain: void main(String argv[]) is not defined
Arguments to the main Method
As you can see from the following code snippet, the main method accepts a single argument: an array of elements of type String.
public static void main(String[] args)
This array is the mechanism through which the runtime system passes information to your application. Each String in the array is called a command-line argument. Command-line arguments let users affect the operation of the application without recompiling it. For example, a sorting program might allow the user to specify that the data be sorted in descending order with this command-line argument:
-descending
for more info
http://journals.ecs.soton.ac.uk/java/tutorial/getStarted/application/main.html
Upvotes: 12
Reputation: 8601
main() is the starting point of an application. When application launches, this function is what is very first evaluated from your code. It is responsible of running your application.
Upvotes: 1
Reputation: 308041
You can compile any Java class without a main
method, but a standalone application can't run without a main()
method *.
The main
method is the method that's defined to be called at the start of an application. Without it, there is no place to start running.
* well, there are ugly hacks where you can do it, but that's cheating
Upvotes: 5
Reputation: 59178
When a program starts running, it has to start execution from somewhere. That somewhere is called main
.
Upvotes: 7