• 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

why main() is static ?

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ALl!
I know you may wonder that this is not an exam objective but really when i think about it I don't have a direct reason for the main() method to be static .

Need your suggestions
Thanks.
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JVM calls the main method which is the entry point of the application.

I guess the main method is static because the JVM does not have to create the object of the class in which main method is written.So it can directly call the main method of your class.

Any other reasons ? Please comment.
 
Mostafa Radwan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jay !

I understand your comment and I need more! :roll:
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider a class that extends Thread and also contains main().

Do we really want the jvm to create an extra instance of our class that has no other purpose but to contain main()? What should happen in a constructor that does useful work like calling start()? We don't want the jvm, which has already started the main thread, starting another thread prematurely.

Of course, we could require main() to be in a separate class with no other purpose, but then what is the point of instantiating it? Methods like main() that serve the overall purposes of the class or application and are not specifiically directed at one instance should be static.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static means a method that can be called without object instantiation.Main is the entry point of a java program at that time JVM cant initialise the oblect thats why main should be Static .
I hope u got it???
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic