Thank you for visiting Blazing Games

Structure of an Application

You may wish to look at the source code (see the sidebar on the previous page for the download link and instructions) while you are reading through this. This section will describe the general structure of an application. A more detailed explanation of all the topics covered here will be presented, so if at first you don't understand, keep reading as this section is meant to be an overview. An application is essentially a class but with the addition of a main method.

At the top of the file you have your package statement. This is an optional statement which tells the compiler what package the class belongs to. A package is essentially a directory where the class is located. If you had a class named X in the package a.b.c, you could have another class named X but in the package a.b.d. To access the class you could use the full package name (a.b.c.X or a.b.d.X) but could also import the class so you only need to use X to refer to the class. It should be pointed out that if you have more then one class named something, only one of them should be imported, while the other one should be accessed by using the full package name. All classes within the package that is used are automatically included.

After a package has been assigned to the file, the next thing to do is to import classes that you are going to use. Imports are optional as any class can be accessed by using the package name in front of the class (as in a.b.c.X). In addition to importing a class, it is possible to import an entire package by using the * wildcard. As the java.lang.* and java.math.* packages are automatically imported, the sample program doesn't need to use any import statements. Imports will be discussed in more detail next chapter.

Next in the program comes the class declaration. Classes can be public or package classes. Public classes can be created by any other class, while package classes can only be accessed by other classes within that package. Public classes have the public keyword in front of them while package classes have no keyword in front of them. A pair of curly braces contain the contents of the class. The contents consists of variables and methods.

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.

Previous Chapter 3 Contents Page 3 of 10 Book Contents Next

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