Copyright 2024 - BV TallVision IT

A local class can hold variables, which can be global or instance dependant. Simple example on how to declare a global class variabele on a local class, available from anywhere in your Abap program.

A lot has been said and done with global variables in Abap reports - they can damage the report structure and turn good reports into unreadible spagetti. The Object Oriented alternative ? The CLASS-DATA variabele. Just as stepping stone to get you interested in OO:  

REPORT Z_OO_EXAMPLE_2.

CLASS lcl_controller DEFINITION. 
  PUBLIC SECTION. 
    CLASS-DATA: 
      gv_material type mara-matnr, 
      gt_materials type standard table of mara-matnr. 
ENDCLASS. 

START-OF-SELECTION.

  lcl_controller=>gv_material = '456DEF'. 
  perform some_form. 

FORM some_form. 
  append '123ABC' to 
    lcl_controller=>gt_materials.
ENDFORM. 

The example demonstrates that global variables which are defined with CLASS-DATA on the class PUBLIC SECTION can be addressed from anywhere in the report. In fact: there's even no need for an IMPLEMENTATION of the class, as there are no metods involved here. Do refrain from using FORM routines though, OO reports don't rely on FORM routines at all.

The lcl_controller class now has a global variabele which is accessible in the whole report, yet it belongs to the lcl_controller class. You global variabele actually has a home. It's not one of the long list of homeless globally defined parameters that sometimes makes Abap reports hard to understand, in this example there is a logical place for the global variabele.

Considering this is only the tip of the iceberg of what you can do in object oriented variabele declarations - this is a great feature and from now on it is no longer just the name of a variabele that can be used to describe what it's for. In our example it is the materials list from the controller class, so we know the controller class own it.

To elaborate a bit more on the example above, this is what should be done to avoid ever having to use a FORM routine again:

REPORT Z_OO_EXAMPLE_3.

CLASS lcl_controller DEFINITION. 
  PUBLIC SECTION. 
    CLASS-DATA: 
      gv_material type mara-matnr, 
      gt_materials type standard table of mara-matnr. 
  CLASS-METHODS:
    some_method. 
ENDCLASS. 

CLASS lcl_controller IMPLEMENTATION. 

  method some_method. 
    append '123ABC' to gt_materials. 
  endmethod. 
ENDCLASS. 

START-OF-SELECTION.

  lcl_controller=>gv_material = '456DEF'. 
  lcl_controller=>some_method( ).