MichaelB.
7/28/2018 - 10:14 AM

Create a static-constructor or static method (CLASS-METHODS)

Static constructors are mainly used to initialise static attributes of a class. Similar to a normal constructor but is called and executed automatically the first time a:

  • Class is referenced or object is instantiated from that class
  • Static Attribute of the class is used (gets executed when there is code that references the class data / static attributes)
  • Static Method of the class is used

Must be declared in the PUBLIC SECTION Have the option of IMPORTING parameters. Can only access static attributes of their class if static Is always called class_constructor

You static constructors or methods with CLASS-METHODS. You can call static methods without instantiating an object. They are called the first time the class is referenced or an object is instantiated from it. Are limited to accessing static data. Static(CLASS) methods are referenced with => Non-static methods are referenced with ->

CLASS car DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS class_constructor.      "Static-Constructor

  PRIVATE SECTION.
    CLASS-DATA: carlog TYPE c LENGTH 40.      "Used by the Class_Constructor

ENDCLASS.

CLASS car IMPLEMENTATION.

  METHOD class_constructor.
    carlog = 'Car class has been used'.
    WRITE: / carlog.
  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

  DATA car1 TYPE REF TO car.
  CREATE OBJECT car1
    EXPORTING
      make     = 'AUDI'
      model    = 'A4'
      numseats = 5
      maxspeed = 120.