The Java main method looks like this:
public static void main(String args[])
We've all seen it I'm sure. As you can see it has three modifiers and one parameter, which is a string array. I will explain each, in detail where neccessary, below.
public - well, it has to be accessible outside the class, so it obviously has ot be public or Java cannot "see" it.
void - This means it returns no value. In C++ main can return an int value. This was usually used in unix systems to tell the thread that forked the process t hat is running the program, the exit status. Usually an exit status of 0 indicated normal termination. A non-zero exit status usually signified an abnormal termination with different integers corresponding to different "errors."
static - To understand why the main method must be static think about it this way. If a method is not marked static, then a class instance must be instantiated to call it. If it's static, you can call the method without instantiating an object.
public class SomeClass
{
public int getInt() {return 0;}
public static float getFloat() { return 1.0; }
}
Given this, I can call SomeClass.getFloat() directly, without creating a new instance of SomeClass. But, to call getInt() I must:
SomeClass c = new SomeClass(); c.getInt();
So, when Java looks for the entry point in a program (the main method), it must be able to call it without instantiating an object, hence the method must be static.
String args[] - This is used for command-line arguments. Say I have the following class:
public class Person
{
private String firstName;
private String lastName;
public Person(String firstName, String lastName)
{
this.firstName= firstName;
this.lastName= lastName;
}
public String toString()
{
System.out.printf("Hello. My name is %s %s/n", firstName, lastName);
}
public static void main(String args[])
{
Person p = new Person(args[0], args[1]);
p.toString();
}
}
If I now execute this class from the command-line, as follows, java Person John Smith, then the strings "John" and "Smith" will be passed to main (in the args[] array) in the left-to-right order in which they were written. In other words args[0] == "John" and args[1] == "Smith". So the output of that should be, Hello. My name is John Smith.
As an extra aside, the main method of a class cal be called from another class. That's right. Java uses it as the entry point of a program, but it is still just a static method. So, from within another class I can do the following:
...
String myArgs = new String[] {"John", "Smith"};
Person.main(myArgs);
...
This is equivalent to the previous command-line execution, java Person John Smith, and should output the same thing: Hello. My name is John Smith. I've used this method of calling another program before, but at the moment I can't recall why. It can be useful for testing purposes, I suppose, although many IDE's provide a way to pass in command-line args.
Hopefully someone finds this useful. If you see any errors, please let me know. Thanks,







MultiQuote








|