ABAP 740 – Is CONSTANT not a Static Attribute anymore?

By | April 6, 2016 | ABAP Objects, OO Concepts | 15,984 | 6

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 the different output.

Comparing both outputs next to each other:

ABAP_740_Class_constructor_Ex_1_compare

What ?

 
LOAD-OF-PROGRAM.
  WRITE: / '... LOAD-OF-PROGRAM ...'.
*
START-OF-SELECTION.
  SKIP 2.
  WRITE: / '... START-OF-SELECTION ...'.
  WRITE: / 'lcl_d=>c_d  ..... ', lcl_d=>c_d.
 
ABAP_740_Class_constructor_Ex_1

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 help: https://help.sap.com/abapdocu_750/en/abapclass-methods_constructor.htm

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 Class LCL_D definition

 
*
* ==== 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_b DEFINITION
 

access static attribute

 
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

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.

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

6 Comments

  • Uwe

    I think this is a bug in the kernel. Maybe you should ask this question on SCN and hope Horst K. knows an answer.

  • Uwe

    P.S.: the documentation for CONSTANTS still (NW7.50) says “Constants declared in the declaration part of a class or an interface are static attributes of that class or interface. ”

    Documentation of CLASS_CONSTRUCTOR: “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. ”

    Just tested your example in 7.50: yes, it behaves like you’ve stated -> wrong in my opinion, the class-constructor has to be called.

  • Hello Uwe,

    Thanks for suggestion. I will post this to SCN and see if we reply from SAP on this.

  • Uwe

    Nice. If done, please post the link here so we can find the discussion on SCN.

  • Hey Uwe,

    I posted the article on SCN and got reply from Horst K.
    http://scn.sap.com/community/abap/blog/2016/04/07/abap-740-is-constant-not-a-static-attribute-anymore

    Apparently this change was implemented but document was not updated. I would try to get the link with that documentation and update the article here.

    Thanks.

  • Vishnu Kant R

    Hi NP,

    This is very interesting. This post has made me dig up several things and now everything looks interesting.
    Good find. Thanks for the article.

    KR,
    Vishnu.

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.