tags:

views:

10376

answers:

16

The method signature of a Java main method is:

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

Is there a reason for this method to be static?

+1  A: 

It's just a convention, but probably more convenient than the alternative. With a static main, all you need to know to invoke a Java program is the name and location of a class. If it weren't static, you'd also have to know how to instantiate that class, or require that the class have an empty constructor.

Logan
It's not a convention; it's part of the language specification; the runtime won't recognise a class without a static main method as a valid entry point.
Rob
The language spec itself follows the convention. There is no actual requirement for the Java designers to have opted for requiring a static main. However, as Logan explains, the alternatives are more complicated.
David Arno
+39  A: 

The main() method in C++, C# and Java are static because they can then be invoked by the runtime engine without having to instantiate an instance of the parent class.

Noah Goodrich
Alright but couldn't the runtime instantiate one object of the class? And then call the Main method? Why?
Andrei Rinea
How would the JVM know which constructor to call, if your main class had overloaded constructors? What parameters would it pass?
Jacob
+4  A: 

Before the main method is called, no objects are instantiated. Having the static keyword means the method can be called without creating any objects first.

BlackWasp
Wrong. Or at least very unprecise. public class Main{ static Object object = new Object() { { System.out.println("object created"); } }; public static void main(String[] args) { System.out.println("in main"); }}
eljenso
Fair comment. Technically, I should have said that before the Main method is called, the class containing the main method is not instantiated.
BlackWasp
+6  A: 

Because otherwise, it would need an instance of the object to be executed. But it must be called from scratch, without constructing the object first, since it is usually the task of the main() function (bootstrap), to parse the arguments and construct the object, usually by using these arguments/program parameters.

PhiLho
+7  A: 

If it wasn't, which constructor should be used if there are more than one?

There is more information on the initialization and execution of Java programs available in the Java Language Specification.

Hank
+1  A: 

Applets, midlets, servlets and beans of various kinds are constructed and then have lifecycle methods called on them. Invoking main is all that is ever done to the main class, so there is no need for state to be held in an object that is called multiple times. It's quite normal to pin main on another class (although not a great idea), which would get in the way of using the class to create a main object.

Tom Hawtin - tackline
+1  A: 

It is just a convention. The JVM could certainly deal with non-static main methods if that would have been the convention. After all, you can define a static initializer on your class, and instantiate a zillion objects before ever getting to your main() method.

Tom
+28  A: 

The method is static because otherwise there would be ambiguity: which constructor should be called? Especially if your class looks like this:

public class JavaClass{
  protected JavaClass(int x){}
  public void main(String[] args){
  }
}

Should the JVM call new JavaClass(int)? What should it pass for x?

If not, should the JVM instantiate JavaClass without running any constructor method? I think it shouldn't, because that will special-case your entire class - sometimes you have an instance that hasn't been initialized, and you have to check for it in every method that could be called.

There are just too many edge cases and ambiguities for it to make sense for the JVM to have to instantiate a class before the entry point is called. That's why main is static.

I have no idea why main is always marked public though.

Jacob
code does run before main -- any static initializer
tdavies
Of course, you are correct.public class JavaClass{ private static JavaClass initme = new JavaClass(); public void main(String[] args){ }}initme is initialized before main() is called.However, the main body of my answer still holds.
Jacob
Fixed; thank you, tdavies.
Jacob
+2  A: 

If the main method would not be static, you would need to create an object of your main class from outside the program. How would you want to do that?

micro
+9  A: 

This is just convention. In fact, even the name main(), and the arguments passed in are purely convention.

When you run java.exe (or javaw.exe on Windows), what is really happening is a coupleo of Java Native Interface (JNI) calls. These calls load the DLL that is really the JVM (that's right - java.exe is NOT the JVM). JNI is the tool that we use when we have to bridge between the virtual machine world, and the world of C, C++, etc... The reverse is also true - it is not possible (at least to my knowledge) to actually get a JVM running without using JNI.

Basically, java.exe is a super simple C application that parses the command line, creates a new String array in the JVM to hold those arguments, parses out the class name that you specified as containing main(), uses JNI calls to find the main() method itself, then invokes the main() method, passing in the newly created string array as a parameter. This is very, very much like what you do when you use reflection from Java - it just uses confusingly named native function calls instead.

It would be perfectly legal for you to write your own version of java.exe (the source is distributed with the JDK), and have it do something entirely different. In fact, that's exactly what we do with all of our Java based apps.

Each of our Java apps has its own launcher. We primarily do this so we get our own icon and process name, but it has come in handy in other situations where we want to do something besides the regular main() call to get things going (For example, in one case we are doing COM interoperability, and we actually pass a COM handle into main() instead of a string array).

So, long and short: the reason it is static is b/c that's convenient. The reason it's called 'main' is because it had to be something, and main() is what they did in the old days of C (and in those days, the name of the function was important). I suppose that java.exe could have allowed you to just specify a fully qualified main method name, instead of just the class (java com.myompany.Foo.someSpecialMain) - but that just makes it harder on IDEs to auto-detect the 'launchable' classes in a project.

Kevin Day
John - thanks for the grammar edit!
Kevin Day
A: 

because, a static members are not part of any specific class and that main method, not requires to create its Object, but can still refer to all other classes.

A: 

Actually before knowing this you people should know about types of memories wrto jvm 1)stack (all non static and non-referance types resides) 2)heap (all referance types resides) 3)registars 4)nativemethods 5)context (all static members and variables)

By default every program written with in a class which is referance type any referance type is accessed using it's object only. but before starting program we cant create a object of the class. in jvm context memory is created when class loads into it.And all static members are present in that memory. if we make the main static now it will be in memory and can be accessible to jvm (class.main(..)) so we can call the main method with out need of even need for heap been created.

A: 

static indicates that this method is class method.and called without requirment of any object of class.

debants
A: 

As the execution start of a program from main() and and java is purely object oriented program where the object is declared inside main() that means main() is called before object creation so if main() would non static then to call it there would be needed a object because static means no need of object..........

Bijoy das
A: 

static method doesnt require any object/class, it runs directly so main runs directly

c k ravi
A: 

static method doesnt require any object, it runs directly so main runs directly

pawan