Skip to Content
avatar image
Former Member

Reg: Contructor and methods

Hi all

i am new to abap objects and i am going thro' the sap materials and i am facing some probelm in understanding the constructor part which is also a type of method..can any one please let me know what is the difference between methods and constructors..

Your explanations are greatly welcome instead of links.

Thanks

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

9 Answers

  • Best Answer
    avatar image
    Former Member
    Sep 04, 2008 at 05:43 AM

    Hi,

    actualy constructor is a special method in OOABAP which is used for initialisation and it is always named as constructor.

    Constructors are of 2 type 1. CLASS or Static Constructor

    and 2 . Method Constructor.

    Check the following code for the use and effect of both types of constructors

    REPORT demo_constructor_ANIRBAN.

    ----


    • CLASS cl1 DEFINITION

    ----


    *

    ----


    CLASS cl1 DEFINITION.

    PUBLIC SECTION.

    METHODS:

    add,

    constructor IMPORTING v1 TYPE i

    v2 TYPE i,

    display.

    CLASS-METHODS:

    class_constructor.

    PRIVATE SECTION.

    DATA:

    w_var1 TYPE i,

    w_var2 TYPE i,

    w_var3 TYPE i,

    w_result TYPE i.

    ENDCLASS. "cl1 DEFINITION

    ----


    • CLASS cl1 IMPLEMENTATION

    ----


    *

    ----


    CLASS cl1 IMPLEMENTATION.

    METHOD constructor.

    w_var1 = v1.

    w_var2 = v2.

    ENDMETHOD. "constructor

    METHOD class_constructor.

    WRITE:

    / 'Tihs is a class or static constructor.'.

    ENDMETHOD. "class_constructor

    METHOD add.

    w_result = w_var1 + w_var2.

    ENDMETHOD. "add

    METHOD display.

    WRITE:

    /'The result is = ',w_result.

    ENDMETHOD. "display

    endclass.

    " Main program----


    data:

    ref1 type ref to cl1.

    parameters:

    w_var1 type i,

    w_var2 type i.

    start-of-selection.

    create object ref1 exporting v1 = w_var1

    v2 = w_var2.

    ref1->add( ).

    ref1->d isplay( ).

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Sep 04, 2008 at 05:45 AM

    Hi,

    Constructor is a special instance method, which is called at runtime when object is created. Each class can have only one instance constructor.

    Methods are internal procedures in classes that determine the behavior of the objects. They can access all attributes in their class.

    Regards

    Abhijeet

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Sep 04, 2008 at 09:53 AM

    Hi,

    Constructors:

    They are special kind of methods which are mainly used to initialise the values or the data in an object. Constructors are implicitly called as and when an object of the clas is created. We in Most of the cases does not call constructors.

    Methods:

    They are code block which are explictly called to perform some functionality.

    Regards

    Sumit Agarwal

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Sep 04, 2008 at 06:16 AM

    This message was moderated.

    Add comment
    10|10000 characters needed characters exceeded

  • Sep 04, 2008 at 06:18 AM

    This message was moderated.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Sep 04, 2008 at 09:58 AM

    Hello Arun,

    Constructor is a special method, which will be called automatically once when you create a instance of that class.

    For example you have your website and you wan to check no. of hits to our site. You will declare one method in that you will update some static variable. This method will automatically called when ever you create an instance in this case your instance is calling that web site.

    I hope you understand the concept

    Regards,

    Naresh.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Sep 04, 2008 at 09:59 AM

    Hi Arun,

    Please check this thread

    constructor-vs-method-qustion

    Hope this would help you.

    Good luck

    Narin

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Sep 04, 2008 at 11:16 AM

    .

    Edited by: Mahesh Reddy .Pocha on Sep 5, 2008 7:29 AM

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Sep 05, 2008 at 05:50 AM

    ,

    Edited by: Mahesh Reddy .Pocha on Sep 10, 2008 8:37 AM

    Add comment
    10|10000 characters needed characters exceeded