Program Types and Structure
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
Every piece of ABAP code lives inside a program: a unit that the system can load and run. Over the years ABAP has offered several kinds of program, each made for a different job: a report that you start and that prints a list, a screen-driven transaction, a library of callable functions, or a class. This page names each kind, shows what a program is made of inside (a part that declares data, then blocks of code that the system calls), and gives one tiny program in both the old report style and the class style. It is written against Standard ABAP release 8.16 and ABAP for Cloud Development release 9.20 (2608). The program kinds below apply to classic, Standard ABAP, on premise and in a private cloud system. ABAP Cloud development, as in the SAP BTP ABAP environment and the S/4HANA Cloud Public Edition, is built around classes and other development objects; its keyword documentation lists only three program types, and the last body section explains the practical consequence.
Before you read
-
Getting Started: what ABAP is, where it runs and how to try small examples.
-
Later pages of this section cover Classic Tools: SE80, SE38, SE11, SE37 and SE16, where you create these programs, and data declarations (Data Types and Data Objects).
Key terms
-
Program type: an attribute of a program that tells the system how it can be started and what it may contain.
-
Executable program: a program that a user or a background job can start directly, usually to read data and show a list.
-
Processing block: a named section of code that the system calls as a unit, such as a method or an event block.
-
Include program: a piece of source text that cannot run alone and is pasted into other programs.
-
Function group: a container program that holds function modules and the data they share.
-
Class pool: the program that the system generates for a global class.
-
Type group: a program that only holds type definitions and constants for shared use.
The program types
Each program has a type, kept with its other attributes. The type decides which declarations and processing blocks the program can contain and how it can be executed. Standard ABAP knows the following types; the statement that opens the source (the program-initiating statement) names the type.
| Type | Opening statement | Purpose |
|---|---|---|
Executable program |
|
Started with the statement |
Module pool |
|
Started through a transaction code that calls one of its screens (dynpros). It holds the dialog modules for those screens. |
Function group |
|
Holds function modules and the data they share. A function module is called with |
Class pool |
|
Holds one global class. Only methods are possible as processing blocks, and visible methods can be called from other programs. |
Interface pool |
|
Holds one global interface. It contains no processing blocks and cannot be called or executed. |
Subroutine pool |
|
Holds subroutines and methods that other programs call. Subroutines should no longer be created in new programs; use classes. |
Type group |
|
Holds only the statements |
Include program |
none |
Not a program by itself. It is used to split source text into separate repository objects, and its text is inserted into another program where an |
Two points help to keep these straight. First, executable programs and module pools are the types that you start as standalone programs, and a module pool needs screens, which is why it is tied to a transaction code. Second, the other types are containers of reusable parts: callable function modules, a class, an interface, types or plain source text. They are loaded when some other program uses them.
What a program is made of
Whatever its type, the source of a program has the same two broad parts.
-
The declaration part comes first, directly after the program-initiating statement. It holds the definitions and declarations that are valid throughout the program: data types, data objects, and the definitions of interfaces and classes. The order is not fixed, but a statement can only refer to something that is already declared.
-
The processing blocks come after. A processing block is an indivisible unit of code that cannot be nested inside another one, and every statement that is not a declaration belongs to one. There are three kinds: dialog modules (called from a screen), event blocks (called when the runtime reaches a certain event), and procedures, which are methods, function modules and subroutines. The order of the blocks does not matter for execution, but it should help readability.
Statements that are not declarations and stand before the first explicit processing block are not an ordinary place
for logic. In an executable program, the system assigns them to an implicit START-OF-SELECTION event block, and it does
the same for all statements of a program that has no explicit processing block, which is why a very short report can be
written without naming that event. The recommended style is an explicit START-OF-SELECTION block that only calls a
method, with the real logic kept in classes. Inside an event block you cannot declare local data: any declaration there
is global to the program.
Event blocks give an executable program its shape. The reporting events occur in a predefined order and only in
executable programs; the two you need are INITIALIZATION (raised before the selection screen is processed, to
prefill its fields) and START-OF-SELECTION (the standard processing block, raised after the selection screen). The
reporting events GET and END-OF-SELECTION are obsolete and belong to logical databases.
Program attributes
Besides its source, a program has properties that are set when it is created in the relevant tool (Class Builder, Function Builder or ABAP Editor) and can be changed later. The ones that control behaviour are:
-
the program type (executable program, module pool, and so on), described above;
-
the ABAP language version, which decides which language elements and repository objects the program may use and which syntax rules apply;
-
fixed point arithmetic, which makes calculations with packed numbers respect the decimal separator;
-
the logical database, an obsolete way to connect an executable program to a data source.
The documented defaults for a new program are the Standard ABAP language version, fixed point arithmetic switched on and no logical database. The language version is the reason code of different ages can live in one system: a program written for Standard ABAP may use the whole language, while the restricted version ABAP for Cloud Development allows only a small set of language elements and restricts access to repository objects. The exact dialogs differ between the classic tools and ABAP Development Tools, so read the properties of an object in the tool you use.
Example: a minimal executable program
The following report writes one line. It is the smallest useful executable program: the opening REPORT statement
sets the type, and the single WRITE statement forms the entire program body. This listing runs in the local
playground and printed the line below; it also needs only classic ABAP, so it runs on an SAP system with Standard ABAP.
REPORT zhello.
START-OF-SELECTION.
WRITE / 'Hello from the program'.
Output: Hello from the program
Source: START-OF-SELECTION
The START-OF-SELECTION line is optional in such a short report, because a program without explicit processing blocks
gets an implicit one. Writing it makes the structure visible.
The class-based equivalent
The same job in class form needs no report at all. A class can implement the interface IF_OO_ADT_CLASSRUN, and ABAP
Development Tools can then run it as an application (Run As, ABAP Application (Console)), where the method receives a
console object whose write method writes to the Console view. The class holds the logic in a method (a processing
block) and no loose statements exist. This example requires an SAP system; not run here.
CLASS zcl_hello DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
ENDCLASS.
CLASS zcl_hello IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
out->write( 'Hello from the program' ).
ENDMETHOD.
ENDCLASS.
In the Expense claims running example, an executable program would have been the old way to list claims from
ZEXP_CLAIM. The way the example is built in this documentation set is with classes and a RAP business object, and a
class like the one above is the entry point for quick tests while you develop.
Program types in ABAP Cloud
ABAP Cloud uses the restricted language version ABAP for Cloud Development. Only a very restricted set of language elements is supported, access to repository objects is restricted, most development is done in methods (where the stricter syntax rules of classes apply) and almost no obsolete language elements are allowed. The keyword documentation for ABAP Cloud lists only three program types: class pool, interface pool and function pool. Executable programs, module pools, subroutine pools and type pools do not appear among them, so a new ABAP Cloud development is built from global classes and interfaces together with other development objects such as CDS views and behavior definitions, and you run a class from the development tool.
The practical rule for a newcomer is simple. If you build something new, write a class. If you read or maintain code that was written before, expect the program types of the table above, and look at the program type to learn how that code is started.
Older material you may meet
Older books, tutorials and code describe the report as the starting point of every ABAP program, with loose
statements after the declarations, subroutines called with PERFORM, and long reports full of global data. That
style still works in Standard ABAP and you will see it in maintenance work, but it differs from current practice in
three ways. First, new code puts its logic in classes with methods, and keeps programs thin or does without them.
Second, data is declared where it is used, with inline declarations, not only in a block at the top of the program;
this is only possible with the modern syntax, and it is how all current examples are written. Third,
older material treats subroutines as a normal way to split a program, while the current documentation says that
subroutines should no longer be created in new programs and that methods replace them. The ABAP language version and
its checks are what let both styles coexist in one system.
Summary
-
A program is the unit the system loads and runs, and its type decides how it can be started and what it can hold.
-
The types are executable program, module pool, function group, class pool, interface pool, subroutine pool, type group and include program.
-
A program has a declaration part and processing blocks (dialog modules, event blocks and procedures); every non-declarative statement lives in a block.
-
A minimal executable program is a
REPORTstatement and a few statements; the class form usesIF_OO_ADT_CLASSRUNand a method. -
ABAP Cloud documents only class pools, interface pools and function pools; you write classes and other objects. Older code shows the older types.
Try it
To do this by hand, follow the guide Your First ABAP Program: it writes a class with a run method, runs it and reads the first error message.