ERPClimb logoERPClimb
Modularization
ABAP Developmentbeginner 9 min read

Modularization Part 6: Methods and Local Classes

Preview · a free account unlocks the full lesson

Key takeaway

Write your first classes for modularization: class definition and implementation, instance vs static methods, attributes, constructors, calling methods, and how a class replaces FORM-based report logic.

In this lessonShow sections

Visual overview

Modularization Part 6: Methods and Local Classes — learning sheetIn one line: Write your first classes for modularization: class definition and implementation, instance vs static methods, attributes, constructors, calling methods, and how a class replaces FORM-based report logic.ERPClimb.com / ABAP / LESSON 6/10 / LEARNING SHEETModularization Part 6: Methods and Local Classes1 · IN ONE LINEWrite your first classes for modularization: class definition and implementation, instancevs static methods, attributes, constructors, calling methods, and how a class replacesFORM-based report logic.2 · KEY IDEAS EXPLAINEDDefinition And ImplementationCLASS lcl_flight_service DEFINITION.Instance vs StaticInstance: each object has its own attributes(mv_carrid). Create with CREATE OBJECT lo_serviceEXPORTING iv_carrid = 'LH'. or NEW…Static: CLASS-METHODS / CLASS-DATA, one per class.Good for stateless helpers (conversions,calculations).Calling MethodsClassic: CALL METHOD lo_service->get_flightsRECEIVING rt_flights = lt_flights.Modern functional: lt_flights =lo_service->get_flights( ).Constructorsconstructor — runs when an object is created;initializes attributes.class_constructor — runs once before first use ofthe class (static initialization).3 · DO · AVOIDBest practice!Common mistakeSmall methods with clear parameters.Private attributes, public methods.Static methods for stateless helpers;instances for stateful services.✕Making everything static even when state ortest replacement is needed.✕Public attributes that anyone can change.✕Huge methods that are just FORMs renamed.4 · IN A REAL PROJECTA team's reports used dozens of FORM routines sharing global tables. When two reports needed the same"flight availability" logic, it was copied, and the copies drifted apart. They moved the logic intoa small global class ZCL_FLIGHT_SERVICE with instance methods for data access and static helpers for…Original ERPClimb learning sheet • Verify against your SAP release and edition

Explanation

Parts 1 through 5 covered classic units like FORMs. Classes are the modern way to modularize code, and this part is the practical introduction — the OOP ABAP topic goes deeper into inheritance, interfaces and polymorphism later on.

A class has a DEFINITION (what) and an IMPLEMENTATION (how). For example: CLASS lcl_flight_service DEFINITION. opens with a PUBLIC SECTION declaring METHODS constructor IMPORTING iv_carrid TYPE s_carr_id. and METHODS get_flights RETURNING VALUE(rt_flights) TYPE ty_flights., followed by a PRIVATE SECTION with DATA mv_carrid TYPE s_carr_id., before ENDCLASS. The matching CLASS lcl_flight_service IMPLEMENTATION. then fills in METHOD constructor. mv_carrid = iv_carrid. ENDMETHOD. and METHOD get_flights. SELECT … WHERE carrid = mv_carrid. ENDMETHOD., closed with ENDCLASS.

There's a real distinction between instance and static members. An instance method belongs to an object, and each object carries its own attributes — mv_carrid above is a good example — created with CREATE OBJECT lo_service EXPORTING iv_carrid = 'LH'. or the more compact NEW lcl_flight_service( 'LH' ).

A static method, declared with CLASS-METHODS, or static data, declared with CLASS-DATA, exists once per class rather than once per object, which suits stateless helpers like conversions or calculations well. Instance methods make sense specifically when the object holds its own state, or needs to be swappable for a test double.

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.