Forums Register Login

main with static

+Pie Number of slices to send: Send
y we use static in our main program
+Pie Number of slices to send: Send
If the method were not static, you would need an object to invoke it with. But when an application is started, no object exists.
+Pie Number of slices to send: Send
Moving this to Java in General -- Beginner. As this forum is for threading issues.

Henry
+Pie Number of slices to send: Send
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 ]
What's a year in metric? Do you know this metric stuff tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 720 times.
Similar Threads
how to complie/run a java prog with 2 or more classes
Why this snippet does not run?
reserved keywords
main()
class 1
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 04:27:50.