Skip to Content

What is the lifetime of a static attribute in an ABAP class?

Hi,

I was always under the impression that the life time of a static attribute in ABAP equated to the the life time of a single user session. I performed an experiment where I created class with a static attribute. I then created a test program where I created 2 instances of this class and displayed the contents of this static attribute. As expected, when I set the value in the static attribute for this class, both objects contained the value that I set for this attribute. I then ran another test program that I had previous created where I just displayed the value of the static attribute for this class. It did not contain a value. I was in the same user session and never left SE80 where I executed both programs. I was expecting the second program to display the same value of the static attribute of the class that the first program had set. This leads to my question. What is the actual lifetime of a static attribute, and is this documented somewhere by SAP?

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Best Answer
    Posted on Feb 16, 2016 at 02:52 PM

    I was always under the impression that the life time of a static attribute in ABAP equated to the the life time of a single user session.

    I don't think so. The lifetime of static attributes is that of the program runtime. (Read - ABAP Keyword Documentation)

    When you start the second program the class(pool) is loaded again, although you never changed the user session!

    Whenever i am confused i refer to this diagram on memory management - ABAP Keyword Documentation.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Feb 16, 2016 at 02:53 PM

    Hi Gregg,

    An Program runs an in internal session. The class static value is bound to the internal session. An internal session is an instance of the user session (there is also an ABAP session) but stops existing when the program ends.

    If the class constructor is called in two different internal sessions. Both internal session will hold there own reference with own values of the class static. You can not read values from one internal session in an other internal session (read program instance).

    Cheers,

    Henk.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Feb 16, 2016 at 02:54 PM

    Hi Gregg,

    Unfortunately I can't cut and paste from here, but the answer is available in SAP Help. Search Google using SAP HELP OBJECT HANDLING, and select the second entry.

    From what I understand changing the an attribute to Static makes the class static and therefore can be accessed without instantiating the class using the => notation.

    Rich

    Add a comment
    10|10000 characters needed characters exceeded

    • Hi Gregg,

      No problem and thanks for the clarification.

      I've been watching your posts coming in thanking each person individually for their contribution. That's a rare thing in the internet world. (Mind you it's a rare thing in the real world - ever tried to get your son/daughter to write a thank you letter ??? 😀)

      So thank you for that!

      (no...I don't need a thanks for this one!! 🤣)

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.