-1

Possible Duplicate:
Why is the Java main method static?

JAVA supports reflection still JAVA needs main method to be static why?

Using reflection we can create the object of class and even call methods of that class so why JAVA need main method to be static.

Appreciate your comments on this.

Community
  • 1
  • 1
Amol Fasale
  • 942
  • 1
  • 10
  • 32

5 Answers5

13

The only answer that can be reliably given is "because the JLS says so". Quote:

The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Yeah I agreed to you, I know It need to be public static void but if we get the concept reflection we gotta know that VM even able to create the object of class which contains main method and call it using reflection. so why it needs to be static. – Amol Fasale Jan 14 '13 at 13:28
  • @AmolFasale +1 The assumption is don't gain anything but creating an object (which would have to make a default or fixed constructor) just to start a program. It would make creating more instances of that class complicated. – Peter Lawrey Jan 14 '13 at 13:36
  • @AmolFasale for a program to be executed, it has to have _some_ entry point, and in Java it's the `main()` method of a given class (which can be specified by `Main-Class` in a manifest). Restricting what `main()` can be is what allows the JVM to be able to "run code" at all. Simple as that. – fge Jan 14 '13 at 13:37
2

Well, reflections have been introduced since Java 1.1 only so the initial Java 1.0 standard could not be based on them. They probably had some reflection-like code just to bootstrap and it is not known how complete it was at times when Java was still called Oak.

Also, this may be inherited from C++ and further inherited from C. While C++ could also construct an application object first and call the virtual main() on it, this probably looked too complex and the choice is to use a simple main, familiar to C developers.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
1

Because when you run a program, the runtime environment looks for a method with the signature public static void main (String[] args). Even if reflexion is supported, the runtime still needs to call a class' main method first.

Laf
  • 7,965
  • 4
  • 37
  • 52
0

Public: In order for the JVM to gain access to the main method it has to be public.

Static: As it would be really complex or maybe impossible to foresee your instantiation criteria of the class hosting the main method, it is mandated to be static, enabling the JVM to call it without the need to create an object from the class.

After all, you need a clear contract when defining a language, without such mandated constraints, things might get messy for the JVM designers.

Waleed Almadanat
  • 1,027
  • 10
  • 24
0

Explanation of every keyword in public static void main(String []args)
1.public:
It must be public because JVM is not part of the class, where you define main method.So, It should not be private
2.static:
JVM is not going to construct any object to call themain. And main is not depending upon the class.
3.void:
mainis not going to return any useful information to JVM. The program execution starts from main() and if there is normal exit ends with the last statement in main.In between the lot takes place.
4.main:
It's just a proper noun used to show the importance of the method.
5.Array of Strings: The args passed to the main is one or more words, they may be an int, float, char, or String. Everything above can be represented as Strings.

Hope it helps
Govind Balaji
  • 639
  • 6
  • 17