Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why static there?

 
Anuji Philip
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why java designers at SUN decided that 'static'
should be there with 'public static void main(
String args[])?Why not without static
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
main()is a special method - it's an "entry point." That's what the JVM calls to start your program.
It's a simple, well defined execution start point. If you didn't make it static, that would mean the JVM would have to create an instance of your main-class before it could call the main() method, and this is not only more work, but less flexible because it has to make a lot of assumptions about your initial program state.
It's much simpler to have a well-defined static method that can be called without any class instances, and let the implementor of main() (the programmer) fill in all the little details about what to do one the user thread is executing .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic