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

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 #7: Constructors and Field Initialization

Let’s take a look at another BBj program—one that demonstrates some interesting facts about the order in which constructors execute and fields are initialized. First, in Vehicle.bbj, we create a basic Vehicle class to act as the top level superclass for all of our vehicle types. Then, in ClassicCar.bbj, EnhancedCar.bbj, and RaceCar.bbj, we create classes that extend the Vehicle class, with code that overrides the superclass constructor behavior. Finally, we create the CompareVehicles.bbj code to use the various Vehicle classes and print out information to make it clear how execution proceeds. Again, we show the code for each new source file using a different border color for readability:

Execution

Creating the BBj source files and then running Program 7 (the code in CompareVehicles.bbj) resulted in the following SysConsole output:

Figure 8. The Output from Program 7

Observations

Here are some details that this code demonstrates:

  • In the CompareVehicles.bbj statement:

    transport! = new Vehicle()

    We are invoking the default no-argument constructor for the Vehicle class. This constructor is defined in Vehicle.bbj to print out a line each time it is invoked. This permits you to see (in Figure 8) that Vehicle’s default constructor is being called first each time a Vehicle, or any of its subclasses, are instantiated.

  • Within each class, static fields are initialized in the order in which they appear (from top to bottom)

    • All static fields are initialized before any instance of the class is created.

    • This must be true in order for static field values to be available for use without needing to instantiate an instance of the class.

    • Static fields can have their values changed at any time, either by code in an instance of the class or by code calling static methods on the class.

    • Because #SerialNumber is a static field of the Vehicle class, it is initialized to 0 at the start of the BBj session. Thus the Vehicle class constructors have access to that initialized value regardless of the order of the #SerialNumber static field declaration in the code.

    • If a class’s static fields change values over time, then each instance of that class will use whatever values are set at instantiation time in its constructors.

  • Within each class, the non-static field initializers are executed in the order in which they appear. So when Vehicle.Description$ is initialized, it can (and does) use the already initialized values of the fields Vehicle.Color$ and Vehicle.MaxSpeed.

  • The default no-argument constructor for ClassicCar has been overridden and declared private. CompareVehicles.bbj’s statement: cCar! = new ClassicCar() uses that constructor, resulting in an error and this output in Figure 8:

    ***** WARNING: The ClassicCar() constructor is private

  • The constructor ClassicCar(BBjNumber maxSpeed) sets the values of the #SerialNumber and #MaxSpeed fields. Since the constructor is executed after the field initializers, the values reported in the description by the constructor will continue to reflect the earlier values until code updates #Description$.

  • The constructor EnhancedCar(BBjNumber maxSpeed) explicitly calls a specific superclass constructor. It passes the new maxSpeed value to the superclass constructor using the syntax:

    #super!(maxSpeed)

  • The constructor EnhancedCar(BBjNumber maxSpeed, BBjNumber milesPerGallon) calls a different constructor of EnhancedCar using the syntax:

    #this!(maxSpeed)

    This is what is referred to as chaining constructors (see Order of Execution for more information on the impact of chaining).

  • A constructor can call methods of the class being constructed. So, for example, the constructor EnhancedCar(BBjNumber maxSpeed, BBjNumber milesPerGallon) contains the statement #setDescription(#newDescription()). When the constructor is complete, the value of #Description$ will contain the value returned by #newDescription().

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