Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

main method

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

why does main method always marked as static ?

Why can naot JVM create a object of that class which has main method ?
 
Marshal
Posts: 70347
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic