• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

main method

 
ajay solanki
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 ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49823
69
  • 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.
 
Raghavan Chockalingam
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 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?
 
camilo lopes
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic