Two Laptop Bag
The moose likes Beginning Java and the fly likes main()  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "main()  " Watch "main()  " New topic


ashok ks
Ranch Hand

Joined: Oct 31, 2003
Posts: 55
What is the use of using main() in the java class ?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
There's a wee bit of magic involved. Really it's just a convention established by the inventors of the language. When you start a Java program at the commandline "java ClassName" the Java runtime loads the class and calls the main() method. The runtime also puts any parameters you entered on the commandline into the String[] parameter for main.
So when you see a main() method, it means you can start the class from the command line. Sometimes that means it is "the program of interest" or the thing people actually want to start up from the command line. Sometimes the main method is just for testing. Some classes that you would never run as a program from the command line have test code or examples of how to call methods in the main.
If you're using an IDE like Eclipse you usually have a "run" button of some kind instead of using the command line launch.
hope that helped!

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
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
In short, main() is just the place where the program starts. The only reason it's special is because the people that made Java said so.

Java API Documentation
The Java Tutorial
Sadanand Murthy
Ranch Hand

Joined: Nov 26, 2003
Posts: 382
Originally posted by ashok ks:
What is the use of using main() in the java class ?

Since others have already explained why main() is there, I'll just mention that it is required ONLY if you java class is the entry point to an application. If it is not an application entry point then it will not have main().

Ever Existing, Ever Conscious, Ever-new Bliss
I agree. Here's the link:
subject: main()
It's not a secret anymore!