Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why should always write public static void main( )

 
mohan gavande
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why Should always write in program as
public static void main()

public means access by every class even it is out of package
static means call that class before making object
void u know very well
main should be write from where program is actually starting
 
Mahesh Bhatt
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is right mohan,

You have all the reasons right there, if that is the main method we are talking about, then the parameter list is missing (String args[]) this is to pass some parameters at the time of running the program, also note that you can also have overloaded main methods in your class however the "public static void main(String args[]){} will be the one that will be called when you run your program.

Hope it clears all the things.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The presence of the main method means you can start the class from the command line. If you have a system of many classes you need at least one that can be started this way. All the other classes don't really need a main method.

I used to put a main on a lot of classes that didn't really require one and put some tests in there. Call a few methods, check the results. You may still see some main methods just for testing. But nowadays I've switched to doing that kind of thing from JUnit tests instead.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic