This week's book giveaway is in the Agile forum.
We're giving away four copies of The Software Craftsman and have Sandro Mancuso on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes void ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "void ?" Watch "void ?" New topic

void ?

Lou Maybe

Joined: Nov 26, 2002
Posts: 2
I have a basic understanding of c++ and that about as far as my programming
knowledge extends. I was lead to belive void main was a bad thing yet in every sample program i've seen in java this seems standard practice, why?
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9056
main() a bad thing?

You need to have a method named
public static void main( String[] args )
to start off any java application. You may not need main() in every class in your project, but you certainly need one or your app won't run.

If you only program applets or servlets you could, however, get by without a main() method.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
The main method in Java *has* to have a return value of "void" per specification.
In C/C++, it is a good practice to let main have a return value of int to specify the exit code of the program.
In Java, a program has an exit value of 0 by default - if you want to specify a different value, you need to use System.exit(exitCode).

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: void ?