Aqua Phoenix
     >>  Lectures >>  Java 1  


1.8 Method: public static void main(String[] args) { }

This method is somewhat of a special method. From the previous discussion, we can make the following statements about this method:
  • public: This method is accessible by any classes.
  • static: This method does not require an instantiation of its object.
  • void: This method does not return anything.
  • main: (name of method).
  • String[] args: This is the data type ( String[]) and name (args) of the input variables.

The method is special, because it is the main entry point of any Java program, except for Java applets. (Java applets are covered separately).

Rules and Observations

  • Java applications, whether GUI-related or not all begin in this method.
  • If this method is missing from the main Java program class, then the class cannot be run.
  • This method is only necesary in a class that can actually be executed. In all likelihood, any given Java program only requires one class with this method.
  • This method is public, because it is necessary for the JVM to evaluate it from outside of its class.
  • This method is static, because its object is not instantiated by the JVM. The JVM does not know how to instantiate the object, and thus the static method main is called upon to do any instantiation if necessary.
  • This method will never return anything, because there is no need to return something to the JVM. Instead, the object (and the program altogether) can destroy itself when finished.
  • This method's name main must always be named main, because the JVM expects to find this method by exactly this name.
  • The argument String[] args is of data type String and is passed as an Array object ([]) (more on arrays later). The name of the array is flexible, but the data type must be of type String[].