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:


8 comments:

yektek training said...

nice post thanks for sharing from Swathi

siva sankar Sankar said...

excellent post thanks for sharing from Sankar

simashree said...

Thank you for such guidance about astrology,Best Blog on
vasiyam in chennai

Bhavya Kumar said...

Best blog.Got to learn new things.Thanks for this Blog SAP Success Factors Training in Chennai

sanayamohit 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!!!!!!

Shruti Ramalingam said...

From My search…Creating Experts provides Best SAP Training with real time projects assistance.

Most of the modules are equipped with advance level topics which the student can learn from the basics to the advance level stage.

They also provide placement assistance in leading MNC companies across the globe according to the current requirements.

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

And these are the Best SAP training institute which provides Real Time Hands on Training…

codedion Technologies-9003085882

creating Experts-8122241286

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

Unknown said...

SAP Success Factors Real Time Hands on Training in Chennai…

Don’t always Depend on Training Institute alone.Please aware of Best Trainers too..

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

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

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

Keerthi Suresh said...

SAP Success Factors Real Time Hands on Training in Chennai…

Don’t always Depend on Training Institute alone.Please aware of Best Trainers too..

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

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

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