Class 1, Part 2 |
Intro to Java 30-IT-396 |
|
public static void main(String args[])
{
method
-
This method, with this exact syntax, is called when you start your program.
This must be present in the first class called.
-
When you run java ClassName on the command line, it looks
for this method in the ClassName class.
-
Any arguments you provide on the command line are added to the input parameter,
args[]. Each String, separated by a space, is a new element in the
args[] parameter.
-
Even if you run the program by clicking on an icon, executing a jar, or
executing from and IDE (Forte'), it still looks to this method to start
the class.
-
Thus, you need to put all of your startup logic here.
-
The syntax (not to be confused with sin tax):
-
public: The access modifier.
-
static: A keyword that states that there is only one instance of
this method for all instances of the class. Further, you need not
instantiate a class to use a static method or static variable.
-
Remember the Math object in JavaScript? That is an object composed
of all static methods and variables.
-
Don't get it? It will make more sense as we use it, but feel free
to ask questions if you wish.
-
void: The return type. Void means that this method does
not return anything.
-
main: The method name. java looks for the method with
this name and this signature when starting a class.
-
(String args[]): An array of command line arguments passed
in to the method.
-
{: Marks the beginning of the method (not an emoticon).
The Homestretch

Created by: Brandan Jones
December 17, 2001