File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes main method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "main method" Watch "main method" New topic

main method

ajay solanki
Ranch Hand

Joined: Feb 25, 2008
Posts: 37

why does main method always marked as static ?

Why can naot JVM create a object of that class which has main method ?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46405
Because that is a decision taken when Java was first written.
Probably to make it appear familiar to C and C++ programmers who were used to writing "main" and not having to write "myObject.main()". It would have been possible to run an instance main() method, but that is what they chose.
Raghavan Chockalingam
Ranch Hand

Joined: Dec 20, 2005
Posts: 77
every single code in Java lies in some class. you need to group the first few lines for the start of an application somewhere inside a class probably a method. so you try to put them in main() method of some class and make it static so that you can call it without making an instance of that class.

Ben Souther

Joined: Dec 11, 2004
Posts: 13410

You can't call an instance method until its object has been created.
You you have to call some method in order to create an object.

Without main being static, how would you overcome this Catch 22 situation?

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
camilo lopes
Ranch Hand

Joined: Aug 08, 2007
Posts: 202
the o main is static because you need have one main by class and shared for all.
You can't have one main for each object by example.
static reference = class
instance reference = object

Brazil - Sun Certified Java Programmer - SCJP 5 About Java - Update every Week.
Guide SCJP - tips that you need know
I agree. Here's the link:
subject: main method
It's not a secret anymore!