Originally published at – ABAP 740 – Is CONSTANT not a Static Attribute anymore?
CLASS_CONSTRUCTOR would be called automatically whenever the class would be accessed – either by creation of an instance or accessing any component. But seems like it is changed with ABAP 740.
In article CLASS_CONSTRUCTOR and CONSTRUCTOR: Who comes before whom?, we discussed how both constructors are called at runtime. Before continue reading further, I suggest you to visit the memory lane and refresh your ideas on when CLASS_CONSTRUCTOR is called.
Thanks Ramakrishna Koliparthi for pointing out this odd behavior. I would not have tried this, based on assumption that it would have worked across all versions.
Example 1 in ABAP 740
The same example 1 from article CLASS_CONSTRUCTOR and CONSTRUCTOR: Who comes before whom? generates different output. Comparing both outputs next to each other:
In the earlier version, when constant was accessed the first time, CLASS_CONSTRUCTOR from all the classes involved in the class hierarchy were accessed. But, now in ABAP 740, it doesn’t execute the CLASS_CONSTRUCTOR at all. It doesn’t execute the even the static constructor of that particular class, e.g. LCL_D.
From the keyword help: Constructor
The static constructor is called automatically exactly once per class and internal session before the class is first accessed. An access to the class is the creation of an instance of the class or the addressing of a static component using the class component selector.
But it didn’t trigger as expected!
Adding a “Real” Static Attribute
My thought was that there is something wrong with how CLASS_CONSTRUCTOR is being called. I didn’t trust the highlighted statement from the help. So, to see the behavior and to test what help says is true or not, I added one Static Attribute in the class LCL_D and access that.
|* New Definition for LCL_D
* ==== LCL_D ===== *
CLASS lcl_d DEFINITION INHERITING FROM lcl_c.
CONSTANTS: c_d TYPE char1 VALUE ‘D’.
CLASS-DATA: v_d TYPE char1.
ENDCLASS. “lcl_d DEFINITION
WRITE: / ‘… LOAD-OF-PROGRAM …‘.
WRITE: / ‘… START-OF-SELECTION …’.
WRITE: / ‘LCL_D=>V_D ….. ‘, lcl_d=>v_d.
Now, the output is same as how it was before ABAP 740.
Based on this, and it was working as expected in earlier versions other than ABAP 740, expected behavior was to trigger all the static constructor within the hierarchy, but it did not. So, the question on the subject line – Is Constant NOT a Static Attribute Anymore? Or SAP has changed the way constants are loaded in PXA in ABAP 740. May be all the constants are loaded already and thus no need to load them again, so no CLASS_CONSTRUCTOR call.
From help on CONSTANTS
Constants are stored in the PXA and are available to all programs.
What do you think? Let me know by your comment.
Also, I’m hopping that Horst Keller or someone from his league might be able to shed some lights on this.