ERPClimb logoERPClimb
OOP ABAP
ABAP Developmentintermediate 10 min read

OOP ABAP Part 5: Instance Methods, Static Methods and Constructors

Preview · a free account unlocks the full lesson

Key takeaway

Understand instance vs static components, the instance CONSTRUCTOR and CLASS CONSTRUCTOR, when each runs, and practical patterns such as validating objects at creation and caching customizing in static attributes.

In this lessonShow sections

Visual overview

OOP ABAP Part 5: Instance Methods, Static Methods and Constructors — learning sheetFlow: Run the code example: observe the order of class_constructor and constructor outputs → Create an object for a non-existing flight and catch the constructor exception → Check that the carrier cache is filled only once. Key ideas: INSTANCE COMPONENTS; STATIC COMPONENTS; THE INSTANCE CONSTRUCTOR; THE STATIC CONSTRUCTOR. Watch out: Heavy use of static methods hides dependencies and makes code hard to test.ERPClimb.com / ABAP / LESSON 5/10 / LEARNING SHEETOOP ABAP Part 5: Instance Methods, Static Methods andConstructors1 · HOW IT FLOWS1Run the code exampleobserve the order ofclass_constructor and constructor…2Create an object for anon-existing flight andcatch the constructor…3Check that the carriercache is filled only once2 · KEY IDEAS EXPLAINEDInstance ComponentsWork with object state (attributes of thisobject).Called with lo_obj->method( ).Static ComponentsCLASS-DATA exists once for all objects;CLASS-METHODS are called with zcl_x=>method( )without an object.Static methods can only access static attributesdirectly.The Instance ConstructorRuns automatically during CREATE OBJECT, beforethe reference is returned.Only IMPORTING parameters (and exceptions).The Static ConstructorNo parameters; runs automatically once perinternal session, before the first access to theclass (object creation or static access).Use for one-time initialization, e.g. reading asmall customizing table into CLASS-DATA.3 · REMEMBER · WATCH OUTRememberInstance components (METHODS, DATA) belong to each object; staticcomponents (CLASS-METHODS, CLASS-DATA) belong to the class.CONSTRUCTOR runs once per CREATE OBJECT; CLASS_CONSTRUCTOR runs onceper session before the class is first used.Constructors ensure objects start in a valid state — or raise anexception.!Watch outHeavy use of static methods hidesdependencies and makes code hard totest.4 · IN A REAL PROJECTA utility class read the plant customizing table in a static method on every call. In a program processing80,000 material documents it executed 80,000 identical SELECTs. Moving the read into CLASS_CONSTRUCTOR with ahashed CLASS-DATA cache made the table load once per session. Later, for unit testing, the team replaced the…Original ERPClimb learning sheet • Verify against your SAP release and edition

Explanation

Part 1 introduced constructors briefly; this lesson looks at the instance versus static design decision in more depth. Instance components, meaning ordinary METHODS and DATA, belong to each individual object, while static components, declared with CLASS-METHODS and CLASS-DATA, belong to the class as a whole rather than to any one instance.

CONSTRUCTOR runs exactly once per CREATE OBJECT call, while CLASS_CONSTRUCTOR runs exactly once per session, before the class is used for the first time at all. Constructors exist to ensure objects start out in a valid state, or to raise an exception when they can't.

One caution worth keeping in mind: heavy reliance on static methods tends to hide dependencies and makes code harder to test.

Instance components work with the state of a particular object — its own attributes — and are called with lo_obj->method( ); each object maintains its own independent copy of its instance attributes.

Static components work differently: CLASS-DATA exists exactly once, shared across every object of the class, and CLASS-METHODS are called directly on the class, as in zcl_x=>method( ), with no object needed at all. A static method can only access static attributes directly.

This makes static components well suited to stateless helpers like conversion or formatting routines, factory methods (covered in Part 8), or caches of customizing data that only need to be read once.

Free account

Create a free account to keep reading

You have read the opening of this lesson. A free ERPClimb account unlocks the rest of it, every other lesson and topic, and your learning history, so you can pick up exactly where you stopped. No card needed.

  • Full explanation
  • Code example
  • Real project scenario
  • Common mistakes
  • Best practices
  • Interview angle

One tap with Google works too. Every topic keeps its first lesson open to everyone.

Keep exploring

Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.