• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

main()

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the use of using main() in the java class ?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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().
 
reply
    Bookmark Topic Watch Topic
  • New Topic