• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Why is main method static?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you please explain in java why main method is always static in nature...
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79699
381
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic