Section 0f

Program Organization in Java


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

What a Java File Looks Like

There will be a variety of ways class files will go together but again, under the umbrella of quality structure, there are some practices that will help program code be more readable and usable.

Top of the File

The first item in a class file will identify which package the class resides in. The many opportunities to create projects and programs and as time goes by, there will be some additional components such as accessing text files and other actions. To make sure the environment is prepared for work and for future operations, it is important to follow a standard structure for the projects and files.

The beginning or top of the file will start with a package identifier that allows more than one Java class to work together as a package. The second item or items may be import actions that allow the local class to use tools created by others for certain activities. The following shows a class in the p2_package group and that has access to a variety of tools.

Image of top of Java document

Class Name

Every class will have a name, and every public class, meaning classes that can be accessed by programmers, will have the same name as the file name. The image above shows an EncryptionClass that was inherited from a TwoDimArrayClass. The following shows the TwoDimArrayClass that is not inherited from another class.

Uninherited class image

Constants

Most classes have constants that are used for reference within and sometime outsided the class. The keyword for constant in Java is final and as a note, constants are always shown as all capital letters with underscores between words. Here is an example.

Constants shown in the Java file

There will be more to discuss about the other keywords later in this reference but the important point is to know that the constant values should be placed first within the class.

Member or Instance Variables

Most classes will have values that are shared between all the methods. Again, these will be discussed later in this reference but the point here is that they should be placed after the constants in the class as shown here.

Display of instance variables in Java

Note above that the last of the constants (FAILED_ACCESS) is shown and the member variables or instance variables are provided thereafter.

Constructors

Some classes will only have one or two constructors and other classes will have more. These should follow immediately after the member variables. Note that constructors may have zero or more parameters within the parentheses, and also note that the constructors have the same name as the class itself. All these conditions will be discussed later in this reference.

Image of constructors in java

Member Methods

From this point to the end of the file, member methods are placed in alphabetical order. There may be a few or many member methods for any given class. Here is an example of two of them.

Image of sample member methods

This ends the class organization part but there is one more little side note to discuss. Most classes are built as "tool kits" to be used by programmers, so the classes can be instantiated, or turned into objects as many times as the programmer wishes. However, there are times that a programmer just wants to use one class in a standalone fashion. This leads to static member variable and methods. This will be discussed next.

Static Operations

The standard operation for using Java classes is to create the class as a "tool box" that has helpful utilities in it. For example, a class might store data in an array, or in other kinds of data storage and management tools called data structures. A class might also manage how you get input from a keyboard or a file, or how you display output to a screen or printer. These classes are like hammers and screwdrivers to a contractor. Having a hammer or screwdriver does not solve a problem. However, using these tools, a person can build a house, fix a sink, or add a back deck. In programming with Java, programmers use a main method as a driver to solve problems. Thus, the "normal" way to use Java is to create one or more "toolbox" files that can be used by a main method that resides in its own separate file. And just as a contractor can have a half dozen screwdrivers in her toolbox, these "toolbox" classes can be instantiated or created multiple times to be used for multiple purposes.

However . . .

To get students started in this reference, there will be one main file having the main method in it and variables and methods as needed to solve the first few problems. However, since, unlike the "normal" use of classes as toolboxes, the problem solving process is completely local to the file and can only exist as a single quantity, Java requires that the methods and variables be declared as static. In a nutshell, this means, whatever gets declared or used exists as a solo or unique quantity. It cannot be instantiated multiple times as can other classes.

The ultimate classic example of the use of the static keyword is the declaration of the main method. It is declared as public static void main indicating that there can be one and only one main method for any given program.

As students develop there knowledge across this reference and their practice, the difference between static quantities and standard Object Oriented Programming (OOP) practices will become clear. Enjoy the learning.