Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

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

GreggHinkle
Participant
0 Kudos

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?

1 ACCEPTED SOLUTION

SuhaSaha
Advisor
Advisor
0 Kudos

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.

12 REPLIES 12

SuhaSaha
Advisor
Advisor
0 Kudos

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.

0 Kudos

Thanks Suhas,

I really liked that diagram. It appear then that static attributes only persist in the Internal Session and not the ABAP Session or User Session of Memory that I previously assumed. Thanks for clearing it up for me.

henk_verdaasdonk4
Active Participant
0 Kudos

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.

0 Kudos

Thanks Henk for your response. I previously thought that it extended to ABAP Session or User Session, but I guess that I was mistaken.

former_member182550
Active Contributor
0 Kudos

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

0 Kudos

0 Kudos

Thanks Simone

0 Kudos

Hi Richard,

Thanks for your response. I know the difference between instance attribute and static attributes and how to access them. I was looking for how long static attribute's persist. I know that instance attributes persist only as long as the object for which they are contained exists while static attributes persist a the class level. Previous posters educated me that this persistence only lasts within the Internal Session, not the ABAP or User session.

0 Kudos

Gregg Hinkle wrote:

I know the difference between instance attribute and static attributes and how to access them. I was looking for how long static attribute's persist.

And I was not saying you didn't.... Even though it appears my answer was wrong,  it does give an indication of the lifetime.  Thinking about it though that would only apply to public statics.

0 Kudos

Hi Simone,

Thanks for your reply. However in the documentation that you provided the link for, it only states that the contents of static attributes define the state of the class that is valid for all instances of the class, but it does not state for which memory session that this applies to. That was the reason for my question. However I do appreciate the response.

0 Kudos

Hi Richard,

I did not mean this as a dig. I am sorry if it seemed that way. I was just clarifying the question that I was asking. I do appreciate you taking the time to answer me.

0 Kudos

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!! )