Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to create an class object

Former Member
0 Likes
576

Why we need set the 'Create object xx' between 'class definition' and 'class implementation'??

If i set the 'CREATE OBJECT' under the 'Class implementation' in a report, the system will tell me that: 'Statement is not accessible'.

This is my code:


CLASS lcl_retal DEFINITION.
  PUBLIC SECTION.
    METHODS constructor.
  PRIVATE SECTION.
    DATA: lv_car(10) TYPE c VALUE 'CAR',
                    lv_truck(10) TYPE c VALUE 'TRUCK'.
ENDCLASS.                    "lcl_retal DEFINITION

CLASS lcl_retal IMPLEMENTATION.

  METHOD constructor .
    WRITE: lv_car, space, lv_truck.
  ENDMETHOD. "print_detaile

ENDCLASS.                    "lcl_retal IMPLEMENTATION

DATA: lo_retal TYPE REF TO lcl_retal.
CREATE OBJECT lo_retal.

Edited by: YINYAN LU on Sep 2, 2009 9:14 AM

1 ACCEPTED SOLUTION
Read only

MarcinPciak
Active Contributor
0 Likes
539

Class definition - class implementation is treated by system as declaration part. This is similary considered as DATA, TYPES section.

Every single statement which is not part of declaration/definition is part of program to execute (is automatically embraced to START-OF-SELECTION event). In regular program when you set DATA, TYPES it can both occur in declaration part and in execution part. On the other hand CLASS...ENDCLASS is considered purely as declaration/definition (not executable code itself) so can't be placed in START-OF-SELECTION.

Assuming above, when you place CREATE OBJECT b/w CLASS....DEFINITON - CLASS .... IMPLEMENTATION it is recognized as part of executable code, but is placed within the one not intended to be executed. System can't split this code to declaration/execution part. It must be explicitly pointed. So it must be of strcuture


"------------------------- DECLARATION/DEFINITION ---------------------
CLASS lcl_retal DEFINITION.
  PUBLIC SECTION.
    METHODS constructor.
  PRIVATE SECTION.
    DATA: lv_car(10) TYPE c VALUE 'CAR',
                    lv_truck(10) TYPE c VALUE 'TRUCK'.
ENDCLASS.                    "lcl_retal DEFINITION
 
"<- here still part of this block, so no executable statements allowed 

CLASS lcl_retal IMPLEMENTATION.
 
  METHOD constructor .
    WRITE: lv_car, space, lv_truck.
  ENDMETHOD. "print_detaile
 
ENDCLASS.                    "lcl_retal IMPLEMENTATION

"---------------------------- EXECUTABLE CODE HERE ---------------
DATA: lo_retal TYPE REF TO lcl_retal.
CREATE OBJECT lo_retal.

Hope now it is clear

Regards

Marcin

Why we need set the 'Create object xx' between 'class definition' and 'class implementation'??

If i set the 'CREATE OBJECT' under the 'Class implementation' in a report, the system will tell me that: 'Statement is not accessible'.

This is my code:


CLASS lcl_retal DEFINITION.
  PUBLIC SECTION.
    METHODS constructor.
  PRIVATE SECTION.
    DATA: lv_car(10) TYPE c VALUE 'CAR',
                    lv_truck(10) TYPE c VALUE 'TRUCK'.
ENDCLASS.                    "lcl_retal DEFINITION

CLASS lcl_retal IMPLEMENTATION.

  METHOD constructor .
    WRITE: lv_car, space, lv_truck.
  ENDMETHOD. "print_detaile

ENDCLASS.                    "lcl_retal IMPLEMENTATION

DATA: lo_retal TYPE REF TO lcl_retal.
CREATE OBJECT lo_retal.

Edited by: YINYAN LU on Sep 2, 2009 9:14 AM

2 REPLIES 2
Read only

MarcinPciak
Active Contributor
0 Likes
540

Class definition - class implementation is treated by system as declaration part. This is similary considered as DATA, TYPES section.

Every single statement which is not part of declaration/definition is part of program to execute (is automatically embraced to START-OF-SELECTION event). In regular program when you set DATA, TYPES it can both occur in declaration part and in execution part. On the other hand CLASS...ENDCLASS is considered purely as declaration/definition (not executable code itself) so can't be placed in START-OF-SELECTION.

Assuming above, when you place CREATE OBJECT b/w CLASS....DEFINITON - CLASS .... IMPLEMENTATION it is recognized as part of executable code, but is placed within the one not intended to be executed. System can't split this code to declaration/execution part. It must be explicitly pointed. So it must be of strcuture


"------------------------- DECLARATION/DEFINITION ---------------------
CLASS lcl_retal DEFINITION.
  PUBLIC SECTION.
    METHODS constructor.
  PRIVATE SECTION.
    DATA: lv_car(10) TYPE c VALUE 'CAR',
                    lv_truck(10) TYPE c VALUE 'TRUCK'.
ENDCLASS.                    "lcl_retal DEFINITION
 
"<- here still part of this block, so no executable statements allowed 

CLASS lcl_retal IMPLEMENTATION.
 
  METHOD constructor .
    WRITE: lv_car, space, lv_truck.
  ENDMETHOD. "print_detaile
 
ENDCLASS.                    "lcl_retal IMPLEMENTATION

"---------------------------- EXECUTABLE CODE HERE ---------------
DATA: lo_retal TYPE REF TO lcl_retal.
CREATE OBJECT lo_retal.

Hope now it is clear

Regards

Marcin

Read only

0 Likes
539

HI,

Thanks for your help. This is very useful for me.

Thanks and Best regards,

Yinyan Lu