2

Why should we declare a main method? I know that the main method is the starting point of the class. But why should only we declare it as public static void main(String args[])?

Why can't we declare it as public static void test(String args[])? Is main a key word? How will the JVM know that it will be the starting point?

Roman C
  • 49,761
  • 33
  • 66
  • 176
user2699954
  • 49
  • 1
  • 5

7 Answers7

6

The JVM has to know somehow where the starting point of your program is. The way the designers chose to mark that is to call the entry method main, because the same name is used for that purpose in C (which most programmers were familiar with at the time) and it makes sense. There's nothing particularly magical about the name, they just had to choose something, and that was a practical choice.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
3

When a program begins, it has to begin from somewhere.. Well, this is the main method.


Why static?

  • When the JVM calls the main method, there is no object existing for the class being called. So it has to have static method to allow this from class.

Why public?

  • The main method is called by the JVM to run the method which is outside the scope of project.

Why void?

  • Think about it, once the main method finishes, it doesn't mean that the program finished. If it spawns a new thread it might be that these threads are still running.
Maroun
  • 94,125
  • 30
  • 188
  • 241
1

there can be a number of methods in your class which can be public and static. How can JVM decide which method is the starting program. TO avoid ambiguity, The Java language decided a particular standard for the method to be executed by JVM. This standard says that "a self executable class should have a method with following signature :

public static void main(String[] arg) {  }

Here is how this standard help JVM

Public : it allow JVM to access and execute method of your class static : the method can be executed just by class name(without making object) void : returns nothing ( JVM expects nothing from your method) main : In order to make your class self executable, the JVM look for this method name and life of your program is valid from the first statement of this method to last statement of this method. as soon as the last statement of this method is executed, your program terminates and instance of JVM dies. This name standard also helps JVM to find a particular entry point. If JVM allow any name then it would be multiple entry points and JVM would get confused. So they decided a single entry point and called it main .

Bharat
  • 904
  • 6
  • 19
1

why should we declare main method? I know that main method is the starting point of the class. But why should only we declare as public static void main(String args[])?

Basically, because that is how it is defined. (And the reason they decided to use "main" rather than some other name is that "main" is the name used for the entry point of a C or C++ program.)


Why can't we declare as public static void test(String args[])?

Because then the JVM wouldn't be able to find the entry point. Suppose, hypothetically, that the entry point method could be anything. Now consider this example:

public class Test {
    public static void foo(String[] args) { ... }

    public static void bar(String[] args) { ... }
}

... and this command line ...

$ java Test

Which method gets called? The foo method? The bar method?


Is that main key word?

No. It is just a well known method name. As far as the core Java language is concerned, the main method is just a method with a particular signature. Indeed, you can have other main methods in the same class with different signatures.


How will JVM knows that it will be the starting point?

Because it is specified that the JVM uses a main method as the starting point.


Why should main be present in a Java class?

Well ... you don't have to have a main method in every class ... or indeed any class ... if you have some other way to launch the JVM that uses a different convention for the entry point. But otherwise, you need at least one class with a suitable main method if you want your application to be runnable.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

It is static - Compiler need not create an object to enter into the method void - java is platform independent . Hence if it returns something , it may vary from platform to platform

public - for access

main() is required so that compiler gets a door to enter into your application . As the name implies , it is the main method that should be executed first .

Shuhail Kadavath
  • 448
  • 3
  • 13
0

JVM follows a specification and shares that specification in form JLS with developers. JVM in its specificaton mentions that main method in the following form is the starting point for any standalone java application:

public static void main(String args[])

From the specs:

Hence as developers we should adhere to the JVM specification so that JVM can understand what we are trying to do.

If my answer is not descriptive or understandable then follow the specification:

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

Read more:

http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

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.

Pandiyan Cool
  • 6,381
  • 8
  • 51
  • 87