ABAP OO - Dynamic, Static, Super
In this article we want to show you the difference between dynamic and static classes and what you can do with them. We also look at the various constructors.
In the last article we mainly looked at the different components of the class and looked at a simple inheritance. Here we go a little more in depth on how to work with classes and what the differences in accessibility are.
Static components of a class can always be used without an instance and belong directly to the class, including:
- Static attributes
- Static methods
In order to be able to access a static component, the "Fat Arrow" is used directly after specifying the class. In addition, a small example of a class with all definitions, as you will surely notice, the data definition and the method are given the addition CLASS. With this addition, the connection to the class is given.
CLASS lcl_example DEFINITION. PUBLIC SECTION. TYPES: td_number TYPE p LENGTH 15 DECIMALS 2. CONSTANTS: c_magic_number TYPE td_number VALUE '13.37'. CLASS-DATA: gd_save TYPE abap_bool. CLASS-METHODS: is_number_magic IMPORTING id_number TYPE td_number RETURNING VALUE(rd_result) TYPE abap_bool. ENDCLASS.
After we have defined the definition, we can now access the components without creating an instance of the class:
DATA: ld_one_number TYPE lcl_example=>td_number. IF lcl_example=>gd_save = abap_true. ld_one_number = lcl_example=>c_magic_number. DATA(ld_magic_flag) = lcl_example=>is_number_magic( ld_one_number ). ENDIF.
As you can see, the type can be used as a reference, for the various components within the class. If your class name is very long, your implementations will be correspondingly longer.
Dynamic properties of a class are attributes and methods that can only be used with one instance. Before you can access the various components, you have to do an instantiation beforehand. To do this, we define the following class:
CLASS lcl_example DEFINITION. PUBLIC SECTION. TYPES: tt_codes TYPE STANDARD TABLE OF t001 WITH EMPTY KEY. DATA: md_name TYPE string. METHODS: select_company_codes RETURNING VALUE(rt_codes) TYPE tt_codes. ENDCLASS.
In order to be able to access the class components, we create an instance with NEW and can then use it. Access is via the simple arrow, just as you would otherwise access references.
DATA(lo_instance) = NEW lcl_example( ). lo_instance->md_name = `John Doe`. DATA(lt_codes) = lo_instance->select_company_codes( ).
We can access public variables at any time and, if they are not protected, we can also assign new content. We can also use a public type in a class for a data declaration outside the class. Here static types interact with dynamic methods.
Dynamic vs Static
What do you need the two variants of a class for? First of all, we want to show you the advantages and disadvantages of these variants so that you can roughly classify them.
A static class behaves like a function group, the data in the class are available throughout the session and you do not need an instance for it. For this you can only save data once in the class and you run the risk that the data will still be available if you assume that the class should actually be initialized.
For dynamic objects you first need an instance that you have to create before you can access the attributes and methods. You can, for example, save this instance in a table and create additional instances. The data is in each instance for itself and if the reference to the object is deleted, then all data is cleanly deleted.
Normally you should implement the dynamic variant, as it offers not only reusability but also clean memory management. This way you will find more variants of this form in the system, whereby static objects are not obsolete. They are mainly used for many auxiliary methods and offer the possibility of working with buffers in the class across objects.
When designing a class, it is advisable to use only one of the two variants in a class and to avoid mixed forms, as other colleagues could otherwise have problems using them because they do not know the individual methods.
A specialty in ABAP is that there are two constructors that can be used for different things. The constructor in general is a method of the class that is automatically called when the instance is created or when the class components are used for the first time. It creates the object and initializes the variables or loads data from the database in order to prepare the instance for use.
There is the CLASS_CONSTRUCTOR for the static part of the class. This is always called when the class or a component of it is called. As the name suggests, it is implemented as a static method. Another example class with the implementation can be:
CLASS lcl_static DEFINITION. PUBLIC SECTION. CLASS-DATA: gd_magic_number TYPE i. CLASS-METHODS: class_constructor. ENDCLASS. CLASS lcl_static DEFINITION. METHOD class_constructor. gd_magic_number = gd_magic_number + 1. ENDMETHOD. ENDCLASS.
Next we output the value of the variable three times using the WRITE instruction. The class constructor is run through only once and the value of the number is increased to 1. As long as we don't change the value in the variable, it will always be available constantly.
" Output will always be 1 WRITE: / lcl_static=>gd_magic_number. WRITE: / lcl_static=>gd_magic_number. WRITE: / lcl_static=>gd_magic_number.
Hint: The class constructor cannot have any import parameters and cannot throw exceptions, if it is not implemented in the class, then it is an empty method.
The constructor is always run through when a new instance of an object is created, either via NEW or CREATE OBJECT. We recommend using NEW as it is the new standard. If you want to work with dynamic types at runtime, you have to work with CREATE OBJECT.
As an example, let's define a class with a constructor that takes a number:
CLASS lcl_dynamic DEFINITION. PUBLIC SECTION. DATA: md_magic_number TYPE i. METHODS: constructor IMPORTING id_number TYPE i. ENDCLASS. CLASS lcl_dynamic DEFINITION. METHOD constructor. md_magic_number = id_number. ENDMETHOD. ENDCLASS.
In the next example we create an instance of the class and use the constructor to provide a number that internally initializes the "md_magic_number" variable. When outputting, the variable always has the value that we passed to the constructor when it was created.
" Output will always be the constructor value DATA(lo_instance) = NEW lcl_dynamic( 13 ). WRITE: / lo_instance->md_magic_number.
Hint: The constructor can have import parameters and also throw exceptions if an error occurs. In such cases the instantiation is canceled.
It's super that you want to learn ABAP OO in depth, but of course that doesn't explain what "super" really does. This is a pseudo reference if you want to access the superclass once in an inheritance. In some cases, you even have to do this, otherwise it will lead to a mistake. To do this, we again define and implement a class that is based on the dynamic class of the last section.
CLASS lcl_super DEFINITION INHERITING FROM lcl_dynamic. PUBLIC SECTION. METHODS: constructor IMPORTING id_number TYPE i. ENDCLASS. CLASS lcl_super DEFINITION. METHOD constructor. super->constructor( id_number ). md_magic_number = md_magic_number + 1. ENDMETHOD. ENDCLASS.
We have to call the constructor of the superclass, otherwise an error occurs and the logic in the superclass for initializing the class is not run and the importing parameters are not supplied. The super call is required wherever you want to call a method of the same name, if you have redefined it in the sub-class, for example.
Today the big question was whether dynamic or static classes should be used. Each of the two forms has its advantages and disadvantages and can also be used effectively together. It is also important to use the constructors, since many classes should be initialized at the beginning.