Constructor Methods in ABAP Object-Oriented Programming


Constructor Methods in ABAP Object-Oriented Programming

In ABAP OOP, constructors are special methods that get executed automatically when certain actions take place. They don't need to be explicitly called and serve the purpose of initializing objects or class-level components. There are two types of constructors:

1. Instance Constructor

2. Static Constructor

 1. Instance Constructor

An instance constructor is executed automatically whenever an object of the class is created. It's a method responsible for initializing the attributes of the object. Since it is object-specific, it runs only once for each object during its lifetime.

- Keyword: constructor

- Usage: Used to initialize instance-specific data when an object is created.

- Special Features:

  - Can contain importing parameters and exceptions.

  - Always declared in the public section of the class.

  - No return values are allowed.

  - Executed only once in the lifetime of every object.

 Example Code for Instance Constructor:

abap

REPORT z_sampleprogram3.

CLASS lcl_abc DEFINITION.

  PUBLIC SECTION.

    DATA : userno       TYPE i,

           username(50) TYPE c.

    METHODS: constructor,

             display.

ENDCLASS.

CLASS lcl_abc IMPLEMENTATION.

  METHOD constructor.

    userno = 100.

    username = 'Arun'.

  ENDMETHOD.

  METHOD display.

    WRITE: / userno,

           / username.

  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

data ob type REF TO lcl_abc.

create OBJECT ob.

call METHOD ob->display.

 Explanation:

- When the object ob is created using CREATE OBJECT, the constructor method is executed automatically. It initializes the values userno and username. The method display is called to show the initialized data.

 2. Static Constructor

A static constructor is executed automatically when a class is loaded into memory. Unlike the instance constructor, it is class-specific and not tied to any object. The static constructor runs only once during the lifetime of the class, irrespective of the number of objects created.

- Keyword: class_constructor

- Usage: Used to initialize static components of the class.

- Special Features:

  - Cannot have parameters or exceptions.

  - Always declared in the public section.

  - Executes only once during the lifetime of the class, either when accessing static components or creating the first object of the class.

 Example Code for Static Constructor (Case 1: Access Static Component):

abap

REPORT z_sampleprogram3.

CLASS lcl_abc DEFINITION.

  PUBLIC SECTION.

    CLASS-DATA b TYPE i.

    CLASS-METHODS: class_constructor.

ENDCLASS.

CLASS lcl_abc IMPLEMENTATION.

  METHOD class_constructor.

    WRITE: 'test data'.

  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

lcl_abc=>b = 10.

 Explanation:

- The class_constructor method is automatically executed when you access the static attribute b using lcl_abc=>b.

 Example Code for Static Constructor (Case 2: Object Creation):

abap

REPORT z_sampleprogram3.

CLASS lcl_abc DEFINITION.

  PUBLIC SECTION.

    CLASS-DATA b TYPE i.

    CLASS-METHODS: class_constructor.

ENDCLASS.

CLASS lcl_abc IMPLEMENTATION.

  METHOD class_constructor.

    WRITE: 'test data'.

  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

DATA ob TYPE REF TO lcl_abc.

CREATE OBJECT ob.

 Explanation:

- In this case, the class_constructor is executed when the first object ob is created. After this, only the instance constructor will be executed for subsequent objects.

 When Both Constructors Exist

If both static and instance constructors exist in a class, and the first object is instantiated, SAP will first execute the static constructor followed by the instance constructor. The static constructor is only run once during the class lifetime, whereas the instance constructor will be executed for each new object.

 Important Points to Remember:

- Instance constructor runs every time an object is created.

- Static constructor runs only once, either when a static component is accessed or the first object of the class is instantiated.

- Instance constructors can have importing parameters, whereas static constructors cannot.

- No explicit call is required for constructors—they are triggered automatically.

 Code Summary:

Instance Constructor Example:

abap

REPORT z_sampleprogram3.

CLASS lcl_abc DEFINITION.

  PUBLIC SECTION.

    DATA : userno       TYPE i,

           username(50) TYPE c.

    METHODS: constructor,

      display.

ENDCLASS.

CLASS lcl_abc IMPLEMENTATION.

  METHOD constructor.

    userno = 100.

    username = 'Arun'.

  ENDMETHOD.

  METHOD display.

    WRITE: / userno,

           / username.

  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

data ob type REF TO lcl_abc.

create OBJECT ob.

call METHOD ob->display.

Static Constructor Example:

abap

REPORT z_sampleprogram3.

CLASS lcl_abc DEFINITION.

  PUBLIC SECTION.

    CLASS-DATA b TYPE i.

    CLASS-METHODS: class_constructor.

ENDCLASS.

CLASS lcl_abc IMPLEMENTATION.

  METHOD class_constructor.

    WRITE: 'test data'.

  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

lcl_abc=>b = 10.

This guide covers how instance and static constructors work in ABAP Object-Oriented Programming. By understanding these methods, you can manage class initialization efficiently.

Post a Comment

Previous Post Next Post