Introduction

Before discussing the difference between Local classes and Global class, let’s have a short introduction of the two.

Class

Classes are a blueprint for objects and represent a set of properties or methods common to all objects of the same type. Classes in SAP ABAP can be defined either locally or globally.

Local classes

Local classes hide in a report source whereas Global classes sit themselves out centrally in the class pool within the class library of the R/3 Repository. If you are creating a report to do some specific task without worrying about the outside world, you can create a local class. But if you’re working on a class that may be of interest to other reports, consider creating a global class.

Let see an example of local class implementation below:

*---------------------------------------------------------------------*
*       CLASS MAIN DEFINITION
*---------------------------------------------------------------------*
CLASS main DEFINITION.
  PUBLIC SECTION.

    "// Instance Methods ( Note: we use the statement 'METHODS'
    "// to define an instance method )
    METHODS set_data IMPORTING i_data TYPE string.

    "// Instance Methods ( Note: we use the statement 'CLASS-METHODS'
    "// to define a static method )
    CLASS-METHODS set_classdata IMPORTING i_data TYPE string.

  PROTECTED SECTION.

    "// Instance Attribute ( Note we use the statement 'DATA'
    "// to define an instance attribute )
    DATA attribute TYPE string.
    "// Static Attribute ( Note we use the statement 'CLASS-DATA'
    "// to define a static attribute )
    CLASS-DATA classattribute TYPE string.

  PRIVATE SECTION.

    "// Instace event ( Note we use the statement 'EVENTS'
    "// to define aN instance event )
    EVENTS event EXPORTING value(e_data) TYPE string.

    "// Instace event ( Note we use the statement 'CLASS-EVENTS'
    "// to define a static event )
    CLASS-EVENTS classevent EXPORTING value(e_data) TYPE string.

    "// For more informations about events see the following example:
    "// ABAP Objects - Creating your First Local Class - Using Events

ENDCLASS.                    "main DEFINITION
Class Implementation

Error rendering macro 'code': Invalid value specified for parameter 'lang'
*---------------------------------------------------------------------*
*       CLASS main IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS main IMPLEMENTATION.
  METHOD set_data.
    CONCATENATE 'Instance Attribute value' i_data
                               INTO attribute SEPARATED BY space.
  ENDMETHOD.                    "set_data
  METHOD get_data.
    MOVE attribute TO r_data.
  ENDMETHOD.                    "get_data
  METHOD set_classdata.
    CONCATENATE 'Static Attribute value' i_data
                               INTO classattribute SEPARATED BY space.
  ENDMETHOD.                    "set_classdata
  METHOD get_classdata.
    MOVE main=>classattribute TO r_data.
  ENDMETHOD.                    "get_classdata
  METHOD print_attribute.
    WRITE: lv_data, /.
  ENDMETHOD.                    "print_attribute
  METHOD print_classattribute.
    WRITE: lv_data, /.
  ENDMETHOD.                    "print_classattribute
ENDCLASS.                    "main IMPLEMENTATION

 

When a class is used in an ABAP program, the system first searches for the local class with the specified name, if not found, it looks for a global class.

As the Local class is visible to a particular program, visibility of all the component can be set to PUBLIC. A Local class can be defined in a global class. It is defined in the Local Types section and implemented in the Implementation section of the Class Editor.

Global Class

The Global class is visible to all the programs, visibility to all components are explicitly assigned. The components with PRIVATE visibility are only accessible in the class itself and are not visible to the subclass.

SAP ABAP Global Class Builder

Now, let’s have a look at their difference.

Difference between Local Classes and Global Class

Local Class Global Class
It is defined within a program. It is defined in Class Builder (T-Code SE24).
It can be only used in programs in which it is defined. It is stored in class pools and can be used by any ABAP programs.
It can be designed “more open” as it is only accessible to within the context. It can be designed “less open” as it is accessible to all the ABAP programs.
It can be defined in a global class. It cannot be defined in a local class.

 

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Translate »