Appearance
Mini Lesson — Local Classes and Local Types in ABAP
Learning path
Complete Lesson 4 — Classes, Objects & Encapsulation and its practice and companion first. Follow this guided lesson, then complete the mini lesson practice before continuing to Lesson 5.
This mini lesson prepares you for larger ABAP classes that use helper types and helper classes internally.
The goal is not RAP, CDS, or database access.
The goal is to understand this ABAP Objects structure:
text
Class Pool
│
├─ Global Class
├─ Local Types
├─ Class-Relevant Local Types
└─ Test ClassesYou will build one practical example:
text
Invoice Processing Class
↓
local invoice types
↓
local calculation helperLesson information
| Teacher-led time | About 60 minutes |
| Environment | SAP BTP ABAP Trial |
| Tool | Eclipse + ADT |
| Prerequisite | Lesson 4 — Classes, Objects & Encapsulation: basic global classes and methods |
| Guided example | Invoice Total Calculator |
| Afternoon practice | Expense Claim Processor |
Learning objectives
After this mini lesson, you should be able to:
- explain what a class pool is;
- distinguish a global class from a local class;
- explain the ADT tabs Global Class, Class-Relevant Local Types, Local Types, and Test Classes;
- define a local structured type with
TYPES; - define a local internal-table type;
- define and implement a local helper class;
- instantiate a local class from the implementation of the global class;
- explain why a helper class may remain local;
- explain when a type or class should instead be global.
1. A global ABAP class lives in a class pool
When you create a global ABAP class, ABAP stores it in a program of type:
text
class poolA class pool contains exactly one:
text
global classand can also contain local declarations and implementations.
Conceptually:
text
Class Pool
│
├─ one Global Class
├─ local data types
├─ local classes
├─ local interfaces
└─ local test classesA local class is therefore not a nested class inside the global class.
The correct idea is:
text
local class in the same class pool2. Global class
A global class is a repository object.
Example:
text
ZCL_INVOICE_PROCESSOR_###Other ABAP development objects can use its visible public API.
In ADT, its main source is shown in:
text
Global Class3. Local class
A local class is declared with normal ABAP Objects syntax:
abap
CLASS lcl_helper DEFINITION.
...
ENDCLASS.
CLASS lcl_helper IMPLEMENTATION.
...
ENDCLASS.But its visibility is limited to the ABAP program/class pool where it is declared.
Typical uses:
text
calculation helper
formatter
converter
validator
parser
internal strategy
test helper4. Local data type
A local data type is declared using:
abap
TYPESExample:
abap
TYPES ty_amount TYPE decfloat34.A structured type:
abap
TYPES:
BEGIN OF ty_invoice,
invoice_no TYPE string,
amount TYPE decfloat34,
END OF ty_invoice.A local type is visible only in its program context.
It is not:
text
a Dictionary object
a global class type
a database table5. The ADT class tabs
Open a normal ABAP class in ADT.
The class editor provides tabs including:
text
Global Class
Class-Relevant Local Types
Local Types
Test ClassesThese tabs separate different parts of the class pool.
6. Global Class tab
This contains the global repository class.
Example:
abap
CLASS zcl_invoice_processor_### DEFINITION
PUBLIC
FINAL
CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
ENDCLASS.7. Local Types tab
SAP uses the name:
text
Local Typesfor local definitions that are not referenced by the declaration of the global class.
This is the tab we use today.
Typical contents:
text
TYPES declarations
local class definitions
local class implementations
local interfacesthat are needed only by implementation code.
8. Class-Relevant Local Types tab
SAP uses:
text
Class-Relevant Local Typesfor local definitions that are referenced in the private section of the global class.
Example idea:
abap
PRIVATE SECTION.
DATA calculator TYPE REF TO lcl_calculator.The global class declaration itself refers to:
text
lcl_calculatorso that local definition must already be known when the global class declaration is processed.
You do not need this tab for today's required exercise.
For the first example, the helper exists only inside:
text
method implementation codeso:
text
Local Typesis sufficient.
9. Test Classes tab
This tab contains:
text
ABAP Unit test classesIt is not where ordinary helper classes belong.
We will use it later when the course reaches ABAP Unit.
10. Why use a local helper class?
Suppose an invoice-processing class needs to:
text
store invoice lines
calculate totals
apply a discount rule
display the resultYou could put everything into:
text
IF_OO_ADT_CLASSRUN~MAINBut then one method becomes responsible for:
text
data creation
business calculation
outputA local helper class lets us separate:
text
orchestrationfrom:
text
calculation11. Create the guided global class
Create:
text
ZCL_INVOICE_PROCESSOR_###Description:
text
Invoice ProcessorImplement:
text
IF_OO_ADT_CLASSRUNInitial source:
abap
CLASS zcl_invoice_processor_### DEFINITION
PUBLIC
FINAL
CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
ENDCLASS.
CLASS zcl_invoice_processor_### IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
ENDMETHOD.
ENDCLASS.Activate it.
12. Open Local Types
Select the class-editor tab:
text
Local TypesEverything you add here belongs to the same class pool.
It does not become another global repository object.
13. Define an invoice-line type
Add:
abap
TYPES:
BEGIN OF ty_invoice_line,
description TYPE string,
quantity TYPE i,
unit_price TYPE decfloat34,
END OF ty_invoice_line.This type describes:
text
one invoice line14. Define an invoice-line table type
Add:
abap
TYPES tt_invoice_lines
TYPE STANDARD TABLE OF ty_invoice_line
WITH EMPTY KEY.Now you have:
text
ty_invoice_line
→ one line
tt_invoice_lines
→ many lines15. Define a result type
Add:
abap
TYPES:
BEGIN OF ty_invoice_total,
subtotal TYPE decfloat34,
discount TYPE decfloat34,
total TYPE decfloat34,
END OF ty_invoice_total.The helper class will return this structure.
16. Define the local helper class
Below the types, add:
abap
CLASS lcl_invoice_calculator DEFINITION FINAL.
PUBLIC SECTION.
METHODS calculate
IMPORTING
lines TYPE tt_invoice_lines
RETURNING
VALUE(result) TYPE ty_invoice_total.
ENDCLASS.The local class is:
text
lcl_invoice_calculatorThe lcl_ prefix is a common naming convention, not an ABAP keyword.
17. Implement the local helper class
Add:
abap
CLASS lcl_invoice_calculator IMPLEMENTATION.
METHOD calculate.
LOOP AT lines INTO DATA(line).
result-subtotal =
result-subtotal
+ ( line-quantity * line-unit_price ).
ENDLOOP.
IF result-subtotal >= 1000.
result-discount = result-subtotal * '0.10'.
ELSEIF result-subtotal >= 500.
result-discount = result-subtotal * '0.05'.
ENDIF.
result-total =
result-subtotal - result-discount.
ENDMETHOD.
ENDCLASS.Business rule:
text
Subtotal < 500
→ no discount
Subtotal >= 500
→ 5% discount
Subtotal >= 1000
→ 10% discount18. Use the local types in the global class implementation
Return to:
text
Global ClassInside main:
abap
DATA lines TYPE tt_invoice_lines.
lines = VALUE #(
( description = 'Laptop Dock'
quantity = 2
unit_price = '180.00' )
( description = 'Monitor'
quantity = 3
unit_price = '220.00' )
( description = 'Keyboard'
quantity = 5
unit_price = '70.00' )
).19. Instantiate the local helper
Add:
abap
DATA(calculator) =
NEW lcl_invoice_calculator( ).The global class implementation can use the local class because both belong to the same class pool.
20. Call the helper and output the result
Add:
abap
DATA(total) =
calculator->calculate( lines ).
out->write(
|Subtotal: { total-subtotal }|
).
out->write(
|Discount: { total-discount }|
).
out->write(
|Total: { total-total }|
).Activate and run:
text
F921. What belongs where?
Your class pool now contains:
text
ZCL_INVOICE_PROCESSOR_###
→ global repository class
ty_invoice_line
→ local data type
tt_invoice_lines
→ local table type
ty_invoice_total
→ local data type
lcl_invoice_calculator
→ local classThe global class handles:
text
input
orchestration
outputThe local class handles:
text
invoice calculation22. Why is the calculator local?
Ask:
Does another repository object need to instantiate this helper?
For today's design:
text
noIt is an implementation detail of the invoice processor.
That makes a local class reasonable.
23. When should it become global?
If later many unrelated applications need the same calculator, it may deserve its own global class.
Use a global class when:
text
the logic is genuinely reusable
other objects must instantiate it
it forms part of a stable API
it deserves an independent lifecycleUse a local class when:
text
it supports one class pool
it is an implementation detail
external code should not depend on it24. Local type vs public member type
A type in:
text
Local Typesis not part of the public API of the global class.
A type declared like this:
abap
PUBLIC SECTION.
TYPES ty_total TYPE decfloat34.can be addressed externally as:
text
ZCL_SOMETHING=>TY_TOTALThese are different design choices.
25. Local type vs data object
This defines a type:
abap
TYPES ty_amount TYPE decfloat34.This creates a variable:
abap
DATA amount TYPE ty_amount.Remember:
text
TYPES
→ defines a data type
DATA
→ creates a data object26. Local class vs object
This defines a class:
abap
CLASS lcl_invoice_calculator DEFINITION.This creates an object instance:
abap
DATA(calculator) =
NEW lcl_invoice_calculator( ).27. Common naming conventions
You will often see:
text
lcl_
→ local class
lif_
→ local interface
ty_
→ type
tt_
→ table type
ts_
→ structure typeThese are conventions, not language keywords.
28. Why this matters before larger ABAP classes
A global class can appear small in:
text
Global Classwhile important implementation code exists in:
text
Local TypesSo when reading an unfamiliar class, inspect the class-pool tabs.
That habit becomes important as the course moves into framework-generated and larger implementation classes.
29. Knowledge check
1. What is a class pool?
2. How many global classes does one class pool contain?
3. Can the same class pool contain local classes?
4. Is a local class nested inside the global class?
5. What is the Local Types tab for?
6. What is Class-Relevant Local Types for?
7. What is the difference between ty_invoice_line and a variable of that type?
8. Why is lcl_invoice_calculator local?
9. When might it become a global class?
10. Which tab contains ABAP Unit test classes?
30. One-hour completion checklist
31. End-of-lesson mental model
text
Class Pool
│
├─ Global Class
│ └─ repository class / external API
│
├─ Local Types
│ ├─ local TYPES
│ └─ local helper classes
│
├─ Class-Relevant Local Types
│ └─ local definitions referenced by
│ the global class private declaration
│
└─ Test Classes
└─ ABAP UnitThe key rule is:
Use local classes and local types for implementation details that belong to one ABAP program/class pool and do not need to become part of a reusable global API.
Continue to the mini lesson practice — Expense Claim Processor →