Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why main() method is needed in java main class

Tags:

java

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?

like image 913
amit Avatar asked Sep 16 '11 10:09

amit


People also ask

Why do we need main method in Java?

In any Java program, the main() method is the starting point from where compiler starts program execution. So, the compiler needs to call the main() method. If the main() is allowed to be non-static, then while calling the main() method JVM has to instantiate its class.

Why main method is inside a class in Java?

Because that is how the language was designed. The JVM first loads the class containing the main function and then calls the main() method. Without loading the class, main() cannot be called, i.e, main() doesn't exist without its enclosing class.

What is main () method in Java?

The main() is the starting point for JVM to start execution of a Java program. Without the main() method, JVM will not execute the program. The syntax of the main() method is: public: It is an access specifier.

Does a class always need a main method?

Yes, the Main method is required to run a function although a java class can be without the Main method.


2 Answers

  1. To answer this we need to understand the fantastic Java architecture.
  2. We have Java source code (.java file)
  3. Compiler checks for errors and generates byte code as the .class file
  4. Then class loader loads .class file
  5. After that JVM takes the responsibility

    • a) JVM loads static blocks
    • 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.

  6. After that Java will produce output by dealing with the operating system and hardware
  7. Is my answer is clear enough? If there are any doubts, please comment.
like image 91
Esann Avatar answered Sep 20 '22 18:09

Esann


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

like image 24
Massimiliano Peluso Avatar answered Sep 17 '22 18:09

Massimiliano Peluso