Static versus instance attributes and methods: what can each access, how long does static data live, and when would you make a method static?
Say your answer out loud first.
Practise another way
Instance attributes exist once per object; static attributes, declared with CLASS-DATA, exist once per class in each internal session and are shared by all its objects. Instance methods can use everything; static methods can use only static components and are called with class=>method without an object.
Static methods cannot be redefined, which also makes them hard to replace in unit tests. So I make methods instance by default and keep static for creation methods and small stateless helpers.
Declarations
- Instance:
DATA,METHODS,EVENTS. Each object has its own copy of instance attributes. - Static:
CLASS-DATA,CLASS-METHODS,CLASS-EVENTS, andCONSTANTS(constants are static too). They exist once per class.
Access rules
- An instance method can read and change instance and static attributes and raise all events.
- A static method can only use static components. There is no
me, so instance attributes are out of reach. - Callers use
oref->meth( )for instance methods andclass=>meth( )for static ones. You can call a static method through a reference variable, but Clean ABAP says not to, because it hides that no object is involved.
CLASS lcl_order DEFINITION FINAL.
PUBLIC SECTION.
CLASS-METHODS created_so_far RETURNING VALUE(result) TYPE i.
METHODS constructor IMPORTING id TYPE string.
METHODS describe RETURNING VALUE(result) TYPE string.
PRIVATE SECTION.
CLASS-DATA counter TYPE i. " one per class, per internal session
DATA id TYPE string. " one per object
ENDCLASS.
CLASS lcl_order IMPLEMENTATION.
METHOD constructor.
me->id = id.
counter += 1.
ENDMETHOD.
METHOD created_so_far.
result = counter. " id is not reachable here
ENDMETHOD.
METHOD describe.
result = |Order { id } of { counter }|.
ENDMETHOD.
ENDCLASS.
How long static data lives: for the internal session. Every user session, background job or RFC call that loads the class gets its own copy, initialised by the static constructor on first use.
Static attributes are not shared across users or application servers; for that you need the database or shared objects.
Why instance is the default
- Static methods cannot be redefined in subclasses, so polymorphism does not work for them.
- They are hard to replace with test doubles, because callers are hard-wired to
class=>meth. Clean ABAP's rule: prefer instance to static methods; static mainly for static creation methods (create,for_customer) and small stateless type utility classes that callers would never mock. - Static state carries over between calls within a session, which creates hidden coupling and order-dependent bugs, including in ABAP Unit, where tests in one class share the session.
Good static uses: factory and creation methods, constants, pure helper functions, a singleton's get_instance. Everything with business behaviour belongs on an instance behind an interface.
States the access rules, the internal-session lifetime of static data, that static methods cannot be redefined, and gives a clear rule for when static is acceptable.
Says 'static is shared and faster, so use it for utilities' and believes static attributes are shared across all users.
Believing CLASS-DATA is global for the whole system. It is per internal session, so each user and each job starts with its own value.
- Can a static method call an instance method of the same class?
- What happens to a static counter when two users run the same report?
- Why are static methods a problem for unit tests?
- Can you redefine a static method in a subclass?
1 candidate write-up, 2 official pages and 4 prep guides. Confidence: high. Checked 1 Dec 2026.
- Prep guide Gives the question.
- Prep guide Gives the question.
- Prep guide Gives the question.
- Candidate write-up Gives the question.
- Prep guide Gives the question.
- Official page Backs the answer.
- Official page Backs the answer.