Appearance
Lesson 9 — Static Members, Constants & Utility Classes
Most behavior so far belonged to objects.
Sometimes a value or operation belongs to the class as a whole rather than one object.
ABAP provides:
text
CONSTANTS
CLASS-METHODS
CLASS-DATA
=>Learning objectives
- distinguish instance members from static/class members;
- declare class constants;
- call
CLASS-METHODSwithout creating an object; - use the static component selector
=>; - understand
CLASS-DATAas shared mutable state; - explain why mutable static state should be used sparingly;
- use a stateless utility class appropriately;
- distinguish class constants from local
FINAL(...)values.
1. Create a VAT calculator
Create:
text
ZCL_VAT_CALCULATOR_###2. Class constant
abap
CONSTANTS c_vat_rate
TYPE decfloat34
VALUE '0.23'.A constant belongs to the class and cannot be changed at runtime.
3. Static method
abap
CLASS-METHODS calculate_vat
IMPORTING net_amount TYPE decfloat34
RETURNING VALUE(vat_amount) TYPE decfloat34.Implementation:
abap
METHOD calculate_vat.
vat_amount = net_amount * c_vat_rate.
ENDMETHOD.4. Call with =>
abap
DATA(vat) = zcl_vat_calculator_###=>calculate_vat(
net_amount = CONV decfloat34( '100.00' )
).No object is created.
Static selector:
text
=>Instance selector:
text
->5. Calculate gross total
abap
CLASS-METHODS calculate_total
IMPORTING net_amount TYPE decfloat34
RETURNING VALUE(total) TYPE decfloat34.abap
METHOD calculate_total.
total = net_amount + calculate_vat( net_amount ).
ENDMETHOD.A static method can call another static method of the same class.
6. Class data
For demonstration:
abap
CLASS-DATA calculations_done TYPE i READ-ONLY.Increase it inside calculation methods:
abap
calculations_done += 1.All callers see the same class-level value.
text
not one counter per object
one counter for the class/runtime context7. Mutable static state is a design choice
CLASS-DATA can introduce hidden shared state.
That can make code:
text
harder to test
harder to reason about
more dependent on call orderUse class data when shared class state is genuinely required, not as a replacement for passing data through clear object/method APIs.
A stateless utility class often needs:
text
CONSTANTS
CLASS-METHODSbut no mutable CLASS-DATA.
8. Public read-only class data
READ-ONLY allows external callers to read a public attribute but not assign to it directly.
The class itself can still manage the value internally.
9. Access constants
abap
out->write( zcl_vat_calculator_###=>c_vat_rate ).10. Constants vs FINAL
Class constant:
abap
CONSTANTS c_vat_rate TYPE decfloat34 VALUE '0.23'.- part of the class definition;
- static class component;
- fixed defined value.
Local immutable value:
abap
FINAL(invoice_id) = `INV-1001`.- local data object;
- assigned from an expression;
- cannot be reassigned afterward.
11. Instance vs class members
| Instance | Static/class |
|---|---|
| belongs to one object | belongs to class context |
called with -> | called with => |
METHODS | CLASS-METHODS |
DATA attribute | CLASS-DATA |
12. When is a static utility appropriate?
Good candidate:
text
pure deterministic calculation
no object identity
no instance stateExample:
text
VAT calculation
unit conversion
formatting helperBut if behavior depends on configuration/state/collaborators, an object may be a stronger design.
13. Create the Runner
Create:
text
ZCL_ABAP_L09_###Use:
abap
METHOD if_oo_adt_classrun~main.
FINAL(net_amount) = CONV decfloat34( '100.00' ).
DATA(vat) = zcl_vat_calculator_###=>calculate_vat(
net_amount = net_amount
).
DATA(total) = zcl_vat_calculator_###=>calculate_total(
net_amount = net_amount
).
out->write( |Net: { net_amount }| ).
out->write( |VAT: { vat }| ).
out->write( |Total: { total }| ).
out->write( |Rate: { zcl_vat_calculator_###=>c_vat_rate }| ).
ENDMETHOD.14. Final ABAP-only architecture review
You have now learned:
text
values and control flow
internal tables
structures and keys
methods
classes and objects
encapsulation
collaboration
inheritance
polymorphism
interfaces
static/class members
constantsYou have not yet learned persistence.
All program data so far has come from:
text
literals
constructors
method parameters
object state
internal tablesThe ABAP SQL, CDS & RAP module introduces the system database and ABAP SQL, then builds toward custom persistence and managed business objects.
15. Summary
text
CONSTANTS
CLASS-METHODS
CLASS-DATA
READ-ONLY
=>
FINAL(...)
stateless utility design
shared-state cautionYou are now ready to continue to ABAP SQL, CDS & RAP Lesson 1 — Database Basics, ABAP SQL, and Code Organization.