Thank you for visiting Blazing Games

The main method

As far as applications are concerned, the main method is the most important method. In fact, an application is required to have a main method. The main method is where execution of the program starts. Unlike other methods, the main method always has the format
public static void main(String args[])

Methods use the general format "access_type optional_keywords return_value name_of_method(parameters)" and are commands that the class understands. To use the class, an instance of the class must be created. When a instance of a class is created, all the public methods are available to the class that created the instance. If the created class is part of the same class, then it also has access to the package methods. Protected methods are methods that are only accessible by the instance or by classes that are super classes of the class. Super classes will be discussed in a later chapter. Finally, private methods are only accessible to the instance. The type of methods are determined by the preface words public, protected, and private. If no preface is used, the method is a package method.

You will notice that the main method uses the optional static parameter. This parameter tells the compiler that the method is a static method. This means that the method does not rely on class variables and that the code within the method does not access any non-static methods within this class. This allows the method to be called without actually creating an instance of the class.

Methods can return a variable. In fact, a return variable type must be specified. If no variable is returned, the keyword void is used. Likewise the method can optionally have a series of parameters. The actual code that the method invokes is contained within a pair of curly braces.

Methods can optionally pass parameters. The parameters are contained within the brackets and have the format
variable_type name, second_variable_type second_name, ...
With there being as many parameters as desired being passed. Variables are described in the next section. The main method has only a single variable, called "args", but this variable is a special type of variable known as an array which will be described in a later section. This parameter contains any arguments that are passed to the program through the command line.

Another thing that the main method can be used for is testing. As every class can have a main, why not have a main that runs through a series of tests. This way, when you are creating a class you will not need to write a separate application to test the class, you simply need to run the class as an application.

What is really interesting is the fact that applets do not have or use a main method. This is actually quite convenient as it allows you to create a combination applet/application. The applet portion can be used on web site while the application version can be used for testing or better yet to add features that can not be done in an applet, such as an ability to save the game.

Previous Chapter 3 Contents Page 6 of 10 Book Contents Next

About - Privacy Policy - Contact - Links - FAQ
Copyright © 2005-2006 Blazing Games Inc. All Rights Reserved