Thursday, March 21, 2013

Constructor gets Triggered when Object is Created

Constructor is something which initializes the object. It provides the memory size to the object at the time of creation. In JAVA the name of constructor is the same with the class. Let us suppose we have a class named Test. So in JAVA we can create object in the following way.

Test objTest = new Test();

Here Test is the class, objTest is the name of the object. New is the keyword which indicates the creation of new object and Test() is the default constructor. Here the constructor name is similar with the class. Hence the constructor is created by automatically at the time of creation of object. Moreover we can say that when the object is created the constructor gets automatically triggered.

We can create our own constructor also. The name will be similar there.

Public Test(){

}

Inside the bracket {} we can write our custom function. This is also a default constructor because we are not passing any parameter but it is user driven. Whenever the object is created, it will get triggered. We also have parameterized constructor as follows.

Test objTest = new Test(9,10);
OR
Public Test(int a, int b){

}

Constructor doesn’t return anything. So its return type is nothing. Constructor initializes the object. Hence we can initialize any variable’s value inside the constructor.

The same concept is applied in ABAP. The name of the constructor is CONSTRUCTOR here; not the class name. It is defined with the METHODS statement. To execute the constructor there is no need to call the method. At the time of creating the object the constructor gets triggered.

In the following program we have defined a class cl_construct under which we have declared a constructor by methods statement. We also have a normal method here. At the implementation of this class we have implemented the constructor & normal method. Now under start of selection event we create an object of that cl_construct class. We didn’t call the methods here. At the output we see the constructor gets triggered and the code has been executed.


REPORT  zsr_test.

*----------------------------------------------------------------------*
*       CLASS cl_construct DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_construct DEFINITION.
  PUBLIC SECTION.
    METHODS: m_meth, constructor.
ENDCLASS.                    "cl_construct DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_construct IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_construct IMPLEMENTATION.
  METHOD m_meth.
    WRITE: / 'General Method'.
  ENDMETHOD.                    "m_meth

  METHOD constructor.
    WRITE: / 'Constructor gets triggered object is created'.
  ENDMETHOD.                    "constructor
ENDCLASS.                    "cl_construct IMPLEMENTATION

START-OF-SELECTION.
  DATA obj TYPE REF TO cl_construct.
  CREATE OBJECT obj.

Below is the output:


1 comment:

Anonymous said...

SAP Success Factors Real Time Hands on Training in Chennai...

Don't always Depend on Training Institute Alone and so please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer over SAP Success Factors Means??? Please ready for an DEMO From the Trainer MR.Karthick
CONTACT:8122241286

Both Classroom/Online Training is Available!!!!!!