Skip to Content
Author's profile photo Naimesh Patel

ABAP 740 – Is CONSTANT not a Static Attribute anymore?

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.


Preface

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:

ABAP_740_Class_constructor_Ex_1_compare.png

What ?


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.
 
PUBLIC SECTION.
   
CONSTANTS: c_d TYPE char1 VALUE ‘D’.
   
CLASS-DATA: v_d TYPE char1.
   
CLASS-METHODS: class_constructor.
   
METHODS constructor.
ENDCLASS.                    lcl_d DEFINITION
LOAD-OF-PROGRAM.
 
WRITE: / ‘… LOAD-OF-PROGRAM ….
*
START-OF-SELECTION.
 
SKIP 2.
 
WRITE: / ‘… START-OF-SELECTION …’.
 
WRITE: / ‘LCL_D=>V_D  ….. , lcl_d=>v_d.

  ABAP_740_Class_constructor_Ex_4.png

Now, the output is same as how it was before ABAP 740.

Why?

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.

Assigned Tags

      9 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Horst Keller
      Horst Keller

      Indeed, there was an "internal" optimization between 7.30 and 7.40.

      There is no need to call the static constructor before accessing a constant of the class and it is now  omitted.

      Already in 7.30 you can have the same effect by declaring a local constant with

      CONSTANTS const LIKE class=>const VALUE class=>const .

      and accessing this.

      Of course, a class constant is still a static attribute.

      Unfortunately, this change was not reported to the documentation.

      Now it is and the documentation is adjusted.

      Thanks for notifying ...

      Horst

      Author's profile photo Naimesh Patel
      Naimesh Patel
      Blog Post Author

      Hello Horst,

      Thanks for confirmation and updating the documentation. Can you please share the link if the online documentation was updated?

      Thanks Much,

      Naimesh Patel

      Author's profile photo Horst Keller
      Horst Keller

      That will take some time ...

      Author's profile photo Uwe Fetzer
      Uwe Fetzer

      But shouldn't all static attributes behave the same?

          CONSTANTS c_a TYPE c LENGTH 1 VALUE 'A'.

          CLASS-DATA d_b TYPE c LENGTH 1 VALUE 'B' READ-ONLY.

      Accessing the first one doesn't trigger the class-constructor, accessing the second one does.

      A bit confused now... 😕

      Waiting for the updated documentation. Maybe this will help.  (of course it will 😎 ).

      Author's profile photo Horst Keller
      Horst Keller

      Hi Uwe, should l tell you or do you want to think a little bit about it? 😛

      Hint: What's a constructor good for?

      Author's profile photo Uwe Fetzer
      Uwe Fetzer

      Ha, got it (you know: it's late AND Friday)

      Author's profile photo Mike Pokraka
      Mike Pokraka

      Actually I'm surprised it didn't work like this <7.4

      Makes sense to me: At machine level, I see a constant as being a static piece of info, in simplistic terms "hardcoded" in the compiled code. An attribute is a reference, even if it is filled with a fixed, readonly value.

      Another way to explain it: constants reference the ABAP code itself, whereas evaluating an attribute is referencing a memory area of the class, which causes the class to load.

      Author's profile photo Horst Keller
      Horst Keller

      Yep. In that context, constants can be regarded as types with values. The usage of class types doesn't call the static constructor either.

      Remember, type pools? They can contain types and constants. Also, constants can be used behind the VALUE addition as types behind TYPE.

      Another example, where the limits between types and CONSTANTS vanish are enumerated types. Those declare types with value sets. Coming in ABAP soon ...

      Author's profile photo Former Member
      Former Member

      Both, the constant and the read only variable have in common that their value cannot be changed from outside the class, e.g. a program using the class.

      
      REPORT ztest.
      zcl_class=>c_a = 'C'.
      zcl_class=>d_b = 'C'.
      
      
      
      

      These statements are both not allowed.

      However, zcl_class=>d_b could be changed from inside the class. This means a method, even the class-constructor could set or modify its value.

      
      CLASS zcl_class DEFINITION.
      PUBLIC SECTION.
      CONSTANTS c_a TYPE c LENGTH 1 VALUE 'A'.
      CLASS-DATA d_b TYPE c LENGTH 1 VALUE 'B' READ-ONLY.
      CLASS-METHODS class_constructor.
      ENDCLASS.
      
      
      
      
      
      CLASS zcl_class IMPLEMENTATION.
      METHOD class_constructor.
      zcl_class=>d_b = 'C'.
      ENDMETHOD.
      
      
      
      

      This is the reason why the CLASS-CONSTRUCTOR needs to be called when accessing the READ-ONLY variable.

      Of course, you cannot change the value of a constant, not from outside the class, not from inside the class.

      Someone noticed that even when calling the class-constructor - in no case this will have an effect on the value of the constant. Thus, they came up with the optimization to save runtime, because the class-constructor could look like this:

      METHOD class_constructor.
      WAIT UP TO 10 SECONDS.
      ENDMETHOD.

      The new way of handling access to constants and types within a class is much faster. 😉

      This is my understanding, please correct if I am wro