Monday, February 7, 2011

Working with Constructor

Description of Constructor:

  • Constructor is automatically called when an object created.
  • Constructor is the same name of the class.
  • No return value.
  • With in static method we can only access class attributes.
  • Class-constructor does not have any parameters.
  • Constructor has only import parameters.

Go to SE38 provide program name and property.

Save it.

Provide the logic.

*&---------------------------------------------------------------------*
*& Report ZLOCALCLASS_VARIABLES *
*& *
*&---------------------------------------------------------------------*
*& How to work Constructor *
*& VikramChellappa *
*&---------------------------------------------------------------------*
REPORT  ZLOCALCLASS_VARIABLES.
*OOPS CONSTRUCTOR.
**PROVIDE DATA TYPES "CONSTRUCTOR DOES NOT HAVE ANY EXPORT PARAMETERS.
*DATA: C TYPE I.
*DEFINE THE CLASS.
CLASS CL_LC DEFINITION.
PUBLIC SECTION.
METHODS: CONSTRUCTOR IMPORTING A TYPE I,
* EXPORTING B TYPE I, "IT TAKES ONLY IMPORT PARAMETERS
ANOTHER.
ENDCLASS.
*class implementation.
CLASS CL_LC IMPLEMENTATION.
METHOD CONSTRUCTOR.
WRITE:/ 'THIS IS CONSTRUCTOR METHOD'.
WRITE:/ 'A =', A.
ENDMETHOD.
METHOD ANOTHER.
WRITE:/ 'THIS IS ANOTHER METHOD' COLOR 5.
ENDMETHOD.
ENDCLASS.
*create the object.
DATA OBJ TYPE REF TO CL_LC.
START-OF-SELECTION.
CREATE OBJECT OBJ EXPORTING A = 10.
* IMPORTING B = C.
*call the method.
SKIP 2.
CALL METHOD OBJ->ANOTHER.

Save it, check it, activate it.

Execute it.

Then the output is like this.

No comments:

Tutorials on SAP-ABAP

Adobe Interactive Forms Tutorials

Business Server Pages (BSP)

Userexits/BADIs

Web Dynpro for ABAP (Step by step procedure for web dynpro,Tutorials on Web Dynpro,)

ALV Tutorials

Blog Archive

goodsites