• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is main method static?  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please explain in java why main method is always static in nature...
 
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the Java Language Specification says it has to be. See http://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.1.4
 
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi try reading that article - looks like it was helpful to a few maybe it can help you too!

Why is main static?
 
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That SO article is helpful.
It is static bceause you want to call it without instantiating the class.
It is public because it is called outside the package.
It is void because there is no need to return anything, unlike the C version which can return the error code… where SUCCESS is conventially defined elsewhere as 0.

It would be quite possible for the JVM to call something like that. Because the main method is associated with a class however, you can start different applications if you know the classes with their starting points. Which you couldn't do in C.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a flaw in Java design. Static methods and utility classes should be avoided in object-oriented programming, see this: http://www.yegor256.com/2014/05/05/oop-alternative-to-utility-classes.html Despite all that, Java main(String...) method has to be static. It's just the way it is
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yegor Bugayenko wrote:It's a flaw in Java design.

a FLAW? That's pretty strong language there for something that at its heart is really just an opinion.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!