Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

void ?

 
Lou Maybe
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic