Welcome to SAP Central
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Parameterized constructor in ABAP

Go down

Parameterized constructor in ABAP Empty Parameterized constructor in ABAP

Post  Uma_ABAP Sat Mar 31, 2012 11:29 pm


Like normal methods constructor methods can import parameters ,but since the constructor
methods cannot be called like normal methods ,so how do we pass the parameters to
constructor methods??

We can pass the parameters to a constructor methods during the object creation i.e while
using “create object” statement. A point to note a constructor cannot export any parameter.

Code:


CLASS test DEFINITION.
PUBLIC SECTION.
METHODS: print_private_data,
CONSTRUCTOR IMPORTING num TYPE I. “constructor importing parameter

PRIVATE SECTION.
DATA: private_num TYPE I.

ENDCLASS.

CLASS test IMPLEMENTATION.
METHOD print_private_data.
WRITE:/ private_num.
ENDMETHOD.
METHOD CONSTRUCTOR. “this constructor initializes the private member with the
private_num = num. “ constructor parameter

ENDMETHOD.
ENDCLASS.
************* main program ******************************
START-OF-SELECTION.
DATA: my_obj_ref TYPE REF TO test.
CREATE OBJECT : my_obj_ref EXPORTING num = 2.
CALL METHOD my_obj_ref->print_private_data.

Uma_ABAP

Posts : 56
Join date : 2012-03-31

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum