ABAP Constructors

An ABAP class has two different types of constructors. One is the class constructor, which is triggered bfore any other method of the class is called. This means even though you are only calling a static method, the class constructor is called in beforehand. The instance constructor on the other hand is called, when you instantiate the class for the first time. This means that you will have to call the instance constructor explicitly by instantiating the class and you call the class constructor implicitly by just using a method of the class.

The methods have to follow the naming convention of "class_constructor" and "constructor". For In case you have both methods, the class constructor is called first.

CLASS znewclass DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .
  PUBLIC SECTION.
    CLASS-METHODS: class_constructor.
    CLASS-METHODS: constructor.
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

CLASS znewclass IMPLEMENTATION.
  METHOD class_constructor.
    "This is called before the first method of the class
  ENDMETHOD.
  METHOD constructor.
    "This is called when you instantiate the class
  ENDMETHOD.
ENDCLASS.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19