Special Method: Constructor

 Introduction:   

  • We will cover the following basic concepts:
  1. Understand, what are constructors.
  2. Simple example class, specially designed to understand the basics of Constructors.
  3. Create a program to demonstrate different ways constructors behave.
  4. Why Constructors?
1. So, what are constructors?
  • Let us understand the basics of the constructors by studying simple differences between normal methods and constructors.


2. Let us start our example by creating a HANA Eclipse class with the below structure.
  • We will create the below 4 methods in the visibility PUBLIC SECTION of the CLASS DEFINITION.
  • Create a Special Instance Method Constructor with the name CONSTRUCTOR, this is the default name we use to create an instance constructor.
  • We will also create a Normal Instance Method with the name INSTANCE_METHOD.
  • Create a Special Static Method Constructor with the name CLASS_CONSTRUCTOR, this is the default name we use to create a static constructor.
  • And similarly we will create a Static Method with the name STATIC_METHOD.


  • Now let us implement the methods in the CLASS IMPLEMENTATION Section as shown below.
  • We will implement all the methods even constructors just like the normal methods we have done in the other examples. 


3. Demonstration of the constructor functionality in a program.
  • First let us create an object of the created class ZCLASS_CONSTRUCTOR as shown below.

  • Now lets us save, activate and run the report in the ABAP console.
  • Interestingly we will see in the output below that, just creating an object of the class is enough to call or invoke the execution of the constructor methods.
  • This means we accessed the class by creating an object for the first time and this was enough to call the constructor methods of both types STATIC as well as INSTANCE.

  • Now let us try and call the normal static method STATIC_METHOD using the class component selector (=>).
 
  • As expected we will see that our STATIC_METHOD gets called after the call of our constructors.

  • Now let us comment/remove the code line for object creation and only execute the call of the static method as shown below. 

  • Here we see only static constructor getting called first and then the normal static method.
  • As we are not creating the class object, the INSTANCE constructor does not get called here.

  • Now let us explore more by only calling INSTANCE_METHOD.

  • Interestingly we will see here along with the Instance method/constructor even the STATIC_CONTRUCTOR gets called.
  • And, if we observe closely the static constructor gets called even before the instance constructor.

  • Now let us try and call the constructor methods.
  • We will first call the INSTANCE CONSTRUCTOR method.
  • We will get the below error.

  • Similarly we will try to call the static constructor as shown below.
  • We will get the same error message. 
  • We cannot call a constructor directly like normal methods.


4. So finally, why constructor?
  • As we know, the constructors get called automatically, having no need to call them separately and most importantly they get called before one could call any other normal method.
  • A developer can make use of this behavior of constructors and set the buffer data and get the basic data needed to be used later in the business logic.
  • Can be used to load certain configurations related data, set the flags & get the switches based on which the behavior of our logic can be changed.
  • Also we can use constructors to implement SINGLETON CLASSES, which will be discussed in detail in the separate post related to the Singleton concept.
Example Code:
REPORT zconstructor_pgm.
*Create an object of the class
*DATA(lr_class) = NEW zclass_constructor( ).

*Call the normal static method of the class
*zclass_constructor=>static_method( ).

*Call the instance Method of the class
*DATA(lr_class) = NEW zclass_constructor( ).
*write /.
*lr_class->instance_method( ).

*Now Let us call the constructors of the class
*DATA(lr_class) = NEW zclass_constructor( ).
*write /.
**Instance Constructor
*lr_class->constructor( ).

**Static Constructor
*zclass_constructor=>class_constructor( ).

Comments

Popular posts from this blog

Class Visibility

Class Inheritance