3

Let's look at the method signature of main() in C, C++ and Java without paying much attention to it.

C and C++ take the syntax of the main() function something like the one shown below.

int main(void)
{
     //Elegant code goes here.

     return(0); 
} 

or it can simply be declared void as shown below.

void main(void)
{

     //Elegant code goes here.

} 

The main() function in C and C++ can optionally takes two command-line arguments, if needed.


The signature of the main() method in Java however, something like this.

public static void main(String []args)
{

    //Elegant Java code goes here.
}

Java requires the main() method to be static simply because it was clearly mentioned that it is the first method to be invoked when no objects were created hence, it must be static. The same thing is also applicable to C and C++. There also the main() function is the first function to be invoked still they don't require the main() function to be static. Why? Why did the Java designers take somewhat different view to implement the main() method in Java?

Lion
  • 18,729
  • 22
  • 80
  • 110
  • 1
    `void main(void)` is bad. Although many compilers allow it, it's not covered by the C standard, so it's not portable. I think the C++ standard even forbids it, as it mandates that main have return type int (but I avoid C++ enough to not be sure about that). – Daniel Fischer Nov 11 '11 at 10:19

5 Answers5

6

C has no objects and all methods are static; in C++ one has to declare methods as virtual for them to be not static.

Jilles van Gurp
  • 7,927
  • 4
  • 38
  • 46
  • 1
    It is relevant. C has no concept of a member function, so there's no need (or even possibility) to declare a function static in the Java sense (in C, declaring a function static means it's not visible outside the file it's declared in, not what you'd want for main). In C++, functions(methods) are static by default, so main is declared static there too. – Daniel Fischer Nov 11 '11 at 10:14
5

Because main is member method in case of Java.

Also See

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
4

In java, entire program is encapsulated within a single class. and that class name is usually the name of the program. Static member function of the class has the ability to be called without using any of the object and only one copy of the static member function is shared by all the object. Since main function is single for all the object of your java program and we are not using any object for calling the main function. For this reason main is declared static,

2

Because main is run without the object being instantiated. Therefore is static method.

Codemwnci
  • 54,176
  • 10
  • 96
  • 129
1

If main() is not declared as static, one would have to instantiate the class first to be able to call the method. But to be able to instantiate, one would need to have a running program. This is a catch 22, so the only way to launch the method is having it static.

bos
  • 6,437
  • 3
  • 30
  • 46
  • 1
    It is already mentioned in the question itself with some twist. My question was only that why is such not a case with C and C++? – Lion Nov 11 '11 at 09:59
  • @Lion because those languages are not as clean OOPish speaking as Java (and I'm a big C/C++ fan). C doesn't even know OOP (though you can simulate something which could look like it). In C++ it is like that for C-compatibility. – Flavius Jul 15 '12 at 06:45