aspose file tools*
The moose likes Beginning Java and the fly likes public static void main - Been on my mind Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "public static void main - Been on my mind" Watch "public static void main - Been on my mind" New topic
Author

public static void main - Been on my mind

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Why does java only allow the single main method declaration as the point of entry for an application? Why didn't Sun allow something like:



as well as the one we all know and love:



This is more of a curiosity question. Most C and C++ compilers allow for different main entry definitions.


GenRocket - Experts at Building Test Data
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
It's just a convenience for starting things up. If there were two (or many) possible methods that the JVM might call when starting a class, you'd have to tell it which one to use.

There's nothing magical at all about main(). The only special bit is when you start a class from the command line the JVM calls it.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

It's interesting to look at the "why" here. Note that C and UNIX were born together. The UNIX linker and the C language both had no idea at all of function overloading. Because of this (and to some extent, the cause of this) the symbols that the linker would work with consisted only of function names, not argument lists as in Java method signatures. Therefore, C allows main to have 0 or 2 (or even 3 -- did you know that historically, you could retrieve a pointer to an environment block as the last argument to main() ?) argument, merely because the linker couldn't tell the difference, since it has no idea how many arguments there are anyway. If the runtime library asks for "main", it gets "main", regardless of how many arguments.

C++ supports overloading, and so it can't use plain function names as linker symbols. Instead, it uses name mangling -- a process by which names are decorated with extra data to describe the argument lists. The linker then works with these mangled names. This has many side effects. For example, for a C++ function to be callable from C, it has to be marked as extern "C". This turns off name mangling. Another side effect is that you often can't link C++ object files emitted by different compilers, since the name-mangling scheme (and actually, lots of other stuff) isn't specified by the language standard.

Java's different because everything is specified, by design. And therefore, the signature of main() is specified exactly, too.


[Jess in Action][AskingGoodQuestions]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Very good explanation, with deep knowledge.


http://home.arcor.de/hirnstrom/bewerbung
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Yep Ernest. That pretty much clears it up. Thanks.
 
 
subject: public static void main - Been on my mind