This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

main with static  RSS feed

 
sureesh chowdary
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
y we use static in our main program
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the method were not static, you would need an object to invoke it with. But when an application is started, no object exists.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to Java in General -- Beginner. As this forum is for threading issues.

Henry
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are running your Java application via the java[.exe] program that comes with the JRE or JDK, then it enters via a method called "main" in the class that you specify on the java[.exe] command line. It must be public and static and accept an array of Strings.

The reason for this is that this is what the Java Language Specification requires. There is no more fundamental reason than that the language designers had to choose something and they thought a public static method called "main" would be reasonably obvious and memorable.

The language designers could have chosen something quite different. For instance, they could have said that the class must be Runnable and have a no-args constructor; the java[.exe] would than have instantiated the class and called run(). I think that would have been better, actually, but...

There are other ways to start a Java application that do not go via main(). However, that's probably beyond this Beginner forum.

The answer to many "why does Java ...?" questions is really just "because the spec says so". As a beginner, it is not wise to spend large amounts of time worrying about the "why" questions. Learn to use Java first.
[ October 09, 2007: Message edited by: Peter Chase ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!