Skip to content

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 Classes

You will build one practical example:

text
Invoice Processing Class
        ↓
local invoice types
        ↓
local calculation helper

Lesson information ​

Teacher-led timeAbout 60 minutes
EnvironmentSAP BTP ABAP Trial
ToolEclipse + ADT
PrerequisiteLesson 4 — Classes, Objects & Encapsulation: basic global classes and methods
Guided exampleInvoice Total Calculator
Afternoon practiceExpense 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 pool

A class pool contains exactly one:

text
global class

and can also contain local declarations and implementations.

Conceptually:

text
Class Pool
│
├─ one Global Class
├─ local data types
├─ local classes
├─ local interfaces
└─ local test classes

A local class is therefore not a nested class inside the global class.

The correct idea is:

text
local class in the same class pool

2. 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 Class

3. 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 helper

4. Local data type ​

A local data type is declared using:

abap
TYPES

Example:

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 table

5. 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 Classes

These 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 Types

for 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 interfaces

that are needed only by implementation code.

8. Class-Relevant Local Types tab ​

SAP uses:

text
Class-Relevant Local Types

for 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_calculator

so 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 code

so:

text
Local Types

is sufficient.

9. Test Classes tab ​

This tab contains:

text
ABAP Unit test classes

It 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 result

You could put everything into:

text
IF_OO_ADT_CLASSRUN~MAIN

But then one method becomes responsible for:

text
data creation
business calculation
output

A local helper class lets us separate:

text
orchestration

from:

text
calculation

11. Create the guided global class ​

Create:

text
ZCL_INVOICE_PROCESSOR_###

Description:

text
Invoice Processor

Implement:

text
IF_OO_ADT_CLASSRUN

Initial 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 Types

Everything 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 line

14. 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 lines

15. 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_calculator

The 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% discount

18. Use the local types in the global class implementation ​

Return to:

text
Global Class

Inside 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
F9

21. 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 class

The global class handles:

text
input
orchestration
output

The local class handles:

text
invoice calculation

22. Why is the calculator local? ​

Ask:

Does another repository object need to instantiate this helper?

For today's design:

text
no

It 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 lifecycle

Use a local class when:

text
it supports one class pool
it is an implementation detail
external code should not depend on it

24. Local type vs public member type ​

A type in:

text
Local Types

is 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_TOTAL

These 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 object

26. 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 type

These are conventions, not language keywords.

28. Why this matters before larger ABAP classes ​

A global class can appear small in:

text
Global Class

while important implementation code exists in:

text
Local Types

So 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 Unit

The 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 →