BBj Custom Objects Tutorial: Program #3: The Static Keyword

Example Programs

Here are some example BBj programs. Each example is designed to demonstrate some aspect of using Custom Object Classes in BBj.

The complete source code for each program is included in an appropriately named sub-folder in the zip file you can download here.

Program #3: The Static Keyword

Let’s take a look at a BBj program that demonstrates the Static keyword. Methods and Fields on a BBj Custom Object Class can be defined as static. See the Static Methods and Static Fields sections for details on these special parts of a BBj Custom Object Class. Let’s rewrite our Check class to show an example of code that uses a static method and a static field. Here is the code (along with the PayrollCheck class from Program 2), again with each of the source files shown using a different text color for readability. We have also highlighted the static keyword in yellow to make it easier to see:

Program 3. Accessing a Static Method and Field of the Check Class

As the developer, we need to define the Check class, which we do in the new Check.src file (in the green border above). You may notice a number of differences between this Check.src file and the one in Program 2. Among other things, the Check class now has a static method, getNextCheckNumber(), and a private static field, NextCheckNumber. getNextCheckNumber() is a static method that accesses the static field NextCheckNumber with the “#” prefix mentioned earlier.

We are using the same PayrollCheck.src file from Program 2, so it is not shown here again.

We also need to write the code to access the class’s static method from outside of the class. We do that in the WriteChecks.src file (in the blue border above).

Once again we defined the Check class (and the PayrollCheck class) and the code to use it in separate BBj source files, so that each is available to use independently or together, and so that changes to one file will not accidentally affect the other.

Execution

Creating the two BBj source files and then running Program 3 (the code in WriteChecks.src) resulted in the following SysConsole output:

Figure 3. The Output from Program 3

Observations

There are some details worth pointing out about this code:

  • The static field NextCheckNumber is shared by all instances of the Check class. It can be used or modified by any instance of the Check class, and even accessed by external code (through the static method getNextCheckNumber()).

  • Each PRINT line in WriteChecks.src demonstrates accessing the getNextCheckNumber() static method on the Check class:

    • Check.getNextCheckNumber() shows how to invoke the static method using only the name of the Check class. It is not necessary to instantiate a variable to use this approach.

    • PRINT aCheck!.getNextCheckNumber() shows how to invoke the static method on a variable of the Check class. If you already have a variable you may find this approach convenient.

    • PRINT bCheck!.getNextCheckNumber() shows that you can invoke the static method on every variable of the Check class, and get the shared value.

    • PRINT PayrollCheck.getNextCheckNumber() shows that you can invoke the static method using the name of a subclass (in this case, the PayrollCheck class is a subclass of the Check class). All subclasses inherit the static methods from their superclasses.

  • Static fields provide one way to implement a form of class namespace—having variables which are global to all instances of the class.

  • Static methods give you control over what code can access your static fields outside of the class. They also offer a way to implement class-specific logic.

  • A static method can only access the class’s static fields and methods; it cannot access any non-static fields or methods. This should make sense because the static method can be invoked directly on the class name, with no specific instance of the class guaranteed to exist.

  • Static fields and static methods are static within a BBj session. If WriteChecks.src is run in two distinct BBj sessions, then each session will start with check number 1, and each session will increment its check numbers independently from the other session.

  • Each static field persists its value throughout the lifetime of a BBj session.

Next Step: Example Program #4

BBj Custom Objects Tutorial Contents

BBj Custom Objects Tutorial: Introduction

BBj Custom Objects Tutorial: Interfaces

BBj Custom Objects Tutorial: Classes

BBj Custom Objects Tutorial: Fields

BBj Custom Objects Tutorial: Methods

BBj Custom Objects Tutorial: Using Custom Objects

BBj Custom Objects Tutorial: Program #1 - Writing a Check

BBj Custom Objects Tutorial: Program #2 - Protected and Private Fields

BBj Custom Objects Tutorial: Program #3: The Static Keyword

BBj Custom Objects Tutorial: Program #4 - Error Handling

BBj Custom Objects Tutorial: Program #5 - Class Inheritance

BBj Custom Objects Tutorial: Program #6 - Callback Choices

BBj Custom Objects Tutorial: Program #7 - Constructors and Field Initialization