ABAP Objects for Beginners. Part 3 – Constructor

Today we would cover Constructor, which is heard by All but understood by only Some. Constructor is not specific to ABAP Objects but it is a general concept in any Object Oriented Programming.

Constructors of Classes:

Constructors are​​ special methods​​ that produce a​​ defined initial state​​ for objects and classes.​​ If you are a beginner of OOPs ABAP, then give stress to three words viz​​ Special Method,​​ Defined​​ and​​ Initial.

Just like in your normal report program, when you hit F8 or hit Execute button, the first event to trigger in INITIALIZATION. You can force your program to default some values in INITIALIZATION.​​

Similarly in Web Dynpro ABAP, we have INIT Methods (WDDOINIT). If you want to manipulate the Components or View or Window at the very beginning, you play around in WDDOINIT.

Now coming back to Object Oriented Programming, if you want the Object of the Class to behave in a certain way when it is Instantiated, then you can handle them in CONSTRUCTOR method.​​ The first thing to trigger in OOPs is​​ the CONSTRUCTOR.​​ The name of the method is always CONSTRUCTOR. Let me re-iterate.​​ It is a​​ Special​​ Method to​​ Define​​ the​​ Initial​​ State of your Class Object.

How is the State of an Object determined?

The state of an object is determined by its instance​​ attributes and static​​ attributes. Content can be assigned to attributes using the addition VALUE of the statement DATA.​​ Constructors are necessary when the initial state of an object is to be defined dynamically.

As in regular methods, there are two types of constructor, namely:

  • Static Constructors
  • Instance Constructors

Static Constructor:

Each class has a single static constructor. This is a predefined public static method of the class_constructor class​​ (program below). To use the static constructor, the static​​ method class_constructor must be declared in the public section of the declaration part of the​​ class using the statement CLASS-METHODS and implemented in the implementation part.​​ The static constructor has no interface parameters and cannot raise exceptions.​​ Unless declared explicitly, the static constructor is merely an empty method.

CLASS-METHODS: class_constructor.

Note:​​ Class_constructor may NOT​​ have any parameters or exceptions.

The naming convention must be followed. If we give any other name then that will not be the static constructor.​​

Static Constructor is called before:​​

  1. Any other attributes and methods
  2. Any other static attributes and methods
  3. ​​ Creating of an object
  4. Registering an event handler method.

Instance Constructor:

Each class has one instance constructor. This is a predefined instance method of the class called constructor. To use the instance constructor, the method constructor must be declared in a visibility section of the class using the METHODS​​ statement and implemented in the implementation part.

METHODS : constructor. ( OR )​​

METHODS : constructor IMPORTING a type i.​​ ​​

Note : Constructor can have only importing parameter and exceptions.

* Project : SAP Object Oriented ABAP *
* Description : Demonstration about the Constructors in SAP ABAP *
REPORT yram_ooabap_constructor.

**&&~~ Class Definition
**~~** Declaration of Static  Variable
    CLASS-DATA : lv_name TYPE char20.
**~~** Declaration of Static  Method
    CLASS-METHODS : show_name.
**~~** Declaration of Instance constructor
    METHODS : constructor IMPORTING a TYPE i.
**~~** Declaration of Static constructor
    CLASS-METHODS: class_constructor." IMPORTING a TYPE i .
    "No Declaratons
    "No Declaratons

**&&~~ Class Implementation
**~~** Implementation of method - CLASS_CONSTRUCTOR
  METHOD class_constructor.
    WRITE :/ 'This is the Method - CLASS_CONSTRUCTOR'.

**~~** Implementation of method - CONSTRUCTOR
  METHOD constructor.
    WRITE :/ 'This is the Method - CONSTRUCTOR'.
    WRITE :/5 'A   =', a.
**~~** Implementation of method
  METHOD show_name.
    WRITE :/ 'This is the SHOW_NAME Method'.
    WRITE :/5 lv_name.


  DATA obj1 TYPE REF TO lcl_class.

  lcl_class=>lv_name = 'SAPSPOT.com'.

** Accessing Static Method
  CALL METHOD lcl_class=>show_name.

**&&~~ Creation of the Object -** When you have import Parameters for the
**&&~~ Constructor you must pass those values 

**&&~~when you are creating the Object

Please debug this simple program to check the sequence of Methods triggered one after another.

Output​​ :

Few Commonly Asked Questions on Constructors

What happens when you try to pass​​ parameters to class_constructor?

When the class_constructor ​​ Called ?​​

Whenever you try to​​ create object for that class, or when you are accessing any static variable or static method.

When the constructor gets called ?

Whenever you create an object for that class.

How you will pass values to constructor Parameters?

When you are creating the object.

What happens when you don’t pass parameters to object when constructor is having​​ parameters?

Note:​​ A is importing parameter of constructor.

Can we redefine constructor?​​

A Big No.​​ Check the below error message when we try to force to redefine an Constructor.