Static blocks

Java allows you to define blocks of code as static. Code within static blocks is executed only once, when the class is first loaded. Rhapsody® allows you to add static blocks to classes in your model, and generates appropriate code for such blocks.

Adding static blocks to classes in a model

To add a static block to a class:

  1. Right‑click the class in the browser and select Add New > StaticBlock. (Alternatively, right‑click the class in an object model diagram and select New StaticBlock.)
  2. Open the Features window for the newly created static block, and on the Implementation tab enter the code for the body of the block.

Changing a static block to an operation

To change a static block to an operation, right-click the static block in the browser and select Change To > Primitive Operation.

Note: When you change a static block to an operation, the operation that is created is a static operation.

To change a primitive operation to a static block, right-click the operation in the browser and select Change To > Static Block.

Reverse engineering and roundtripping static blocks

If you reverse engineer code that contains static blocks, Rhapsody recognizes these blocks and adds them to the class in the model.

The roundtripping feature can handle the addition of new static blocks to your code, as well as changes to the body of a static block.

When changing code directly within a static block, keep in mind that when adding code to the body of a static block, the new code is roundtripped into the model only if you placed the code between the Rhapsody annotations inside the block.

If you delete static blocks from your code, the roundtripping behavior depends upon the value of the JAVA_Roundtrip::Update::AcceptChanges property.