11

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?

amit
  • 119
  • 1
  • 1
  • 3
  • "I know we can compile and run a java program successfully without a main() method" ... no, you can't. – Angel O'Sphere Sep 16 '11 at 11:06
  • 1
    "It is door to enter into Java House/Architecture." So question would become why door need to enter the house :) – Oomph Fortuity Aug 23 '18 at 13:23
  • @amit - You can only `compile` but cannot run the class file that don't have the `main` method. You will encounter an error `Main method not found` when you run the file that don't have the `main` method. – Stack Overflow Dec 20 '18 at 07:53

6 Answers6

12

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

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
7

When a program starts running, it has to start execution from somewhere. That somewhere is called main.

Caner
  • 57,267
  • 35
  • 174
  • 180
  • Ok, what if i am using static block, and then quitting JVM by System.exit(1), then we don't need main() here. – amit Sep 16 '11 at 10:44
  • @amit: You cannot rely on static blocks: the execution order of them depends on the class instantiation/referencing order. So if there are several static blocks in different classes, which of them is going to be executed first? Which of them is going to be executed at all? – Vlad Sep 16 '11 at 10:50
7

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
Santosh
  • 17,667
  • 4
  • 54
  • 79
5

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

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • but my question is still answered, we can use static block for all kinda of activities and then using this System.exit() command we can quit off JVM looking for main() method. I want to ask that is there anything that static block can not do but main()? – amit Sep 16 '11 at 10:47
  • How are you planning to get the JVM to start executing with only a static block? There's no way to execute the class. – mcfinnigan Sep 16 '11 at 10:50
  • 1
    @mcfinnigan: Well, basically Java _could_ perform a static initialization of a somehow marked class, and its static blocks will be executed then. The point is that the current convention is different, and IMHO more reasonable. – Vlad Sep 16 '11 at 10:53
  • @Vlad: What entry point would one use into the JVM to do this? My understanding is that you need an application entry point in order to actually spawn the jvm. – mcfinnigan Sep 16 '11 at 10:55
  • @mcfinnigan: currently, you are right. But it would be _theoretically_ possible for Java compiler to create a stub main, which will do nothing but _reference_ some of the classes. This referencing would trigger static initialization, which in turn would start the "real" program. – Vlad Sep 16 '11 at 10:57
  • @Vlad: Fair enough, but the idea gives me chills. I already hate the static keyword. :D – mcfinnigan Sep 16 '11 at 10:59
  • @mcfinnigan: I share your feelings about the idea :) It has many disadvantages. For example, one would need to pass command line arguments through a global variable. And it is in violation of the principle "everything is a class", it introduces back the raw code concept (without even a procedure!), which is a step backwards in my opinion. – Vlad Sep 16 '11 at 11:02
  • @amit a static block can not accept arguments, and depending on the JVM it is not garanteed that a "java MyClass" even executes the static block if it does not find a main method (after all, it is not garanteed that the "class is laoded" if the main method is missing. – Angel O'Sphere Sep 16 '11 at 11:09
2
  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.
anothernode
  • 5,100
  • 13
  • 43
  • 62
Esann
  • 77
  • 5
1

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.

Smar
  • 8,109
  • 3
  • 36
  • 48