When START-OF-SELECTION is Not Required 

Can you why the following program is not working.  If I remove start-of-selection and what is the importance of start-of-selection...... how we come to know that we have to use start-of-selection here...if I put start-of-selection statement in any line apart from that line it shows error, why is it so?

REPORT ZABAPOO .

CLASS RAJ DEFINITION.
PUBLIC SECTION.
DATA: C TYPE I.
METHODS :
SUM
IMPORTING
A TYPE I
B TYPE I
EXPORTING
C TYPE I.
ENDCLASS.

CLASS RAJ IMPLEMENTATION.
METHOD SUM.
C = A + B.
ENDMETHOD.
ENDCLASS.

DATA: V1 TYPE I,
V2 TYPE I,
SUM TYPE I.

DATA: OBJ TYPE REF TO RAJ.
START-OF-SELECTION.

CREATE OBJECT OBJ.
V1 = 2.
V2 = 5.

CALL METHOD OBJ->SUM
EXPORTING
A = V1
B = V2
IMPORTING
C = SUM.

WRITE: SUM.

-----------------

This is just to elaborate on why START-OF-SELECTION is not required if CLASS IMPLEMENTATION is put at the end of the code. 

It is a very basic, yet interesting concept and not relevant to only Object Oriented ABAP.

In an ABAP report, non declarative statements (means other than data: or type stmts)  that are not assigned to a processing block are never executed. Only exception to this are the statements between the REPORT or PROGRAM statement and the first processing block (say a subroutine). These statements are assigned to the default event 
START-OF-SELECTION.

For example see the follwoing code - 

REPORT ZTEST.

* Statements between the REPORT or PROGRAM statement and the first 
* processing block 
WRITE ' Welcome '.

* First processing block 
FORM proc_block1.
WRITE 'Enjoy ABAP'.
ENDFORM.

* Statement not assigned to any processing block - NOT ACCESSIBLE
WRITE / 'Learn ABAP'.
PERFORM proc_block1.

In the above program the 2 nd WRITE statement after the first processing block 
proc_block1 is not executed because it is not assigned to any processing block.
We get a syntax error that "Statement not accessible". 

There are 2 ways to avoid this syntax error - 

Method 1 :

Assign the unaccessible statement to the START-OF-SELECTION block, by writing keyword START-OF-SELECTION just before it. Now START-OF-SELECTION block will contain (statements between the REPORT or PROGRAM statement and the first processing block) & (statements written after keyword START-OF-SELECTION)

REPORT ZTEST.

* Statements between the REPORT or PROGRAM statement and the first
* processing block 
WRITE ' Welcome '.

* First processing block 
FORM proc_block1.
WRITE / 'Enjoy ABAP'.
ENDFORM.

* Assign the unaccessible statement to START OF SELECTION block 
START-OF-SELECTION.
WRITE / 'Learn ABAP'.

PERFORM proc_block1.

Method 2 :

Write the first processing block (FORM proc_block1) at the end i.e. after all statements not assigned to a processing block. So all those statements will now fall under the exception category (i.e. statements between the REPORT or PROGRAM statement and the first processing block) and can be accessed.

REPORT ZTEST.

* Statements between the REPORT or PROGRAM statement and the first
* processing block
WRITE ' Welcome '.

WRITE / 'Learn ABAP'.

PERFORM proc_block1.

* First processing block put after all the statements which are
* not assigned to a processing block
FORM proc_block1.
WRITE / 'Enjoy ABAP'.
ENDFORM.

Now this was a procedural program, thinking from ABAP Objects perspective,
the CLASS IMPLEMENTATION is normally the first processing block (not CLASS DEFINITION because it is just a declaration). So on writing another non declarative statement like CREATE OBJECT after this processing block and not assigning this statement to another processign block, we get same syntax error that 'STATEMENT NOT ACCESSIBLE'. 

To avoid this error we can use 2 ways as discussed above - either assign the statement to a START-OF-SELECTION block or put the first processing block   i.e. CLASS IMPLEMENTATION at the end of the code. 

*-- Ashwini Gangam

Related:

ABAP Books List
ABAP/4 Certification, Programming, Smartforms, Sapscripts and Object Oriented Programming Books

ABAP Menu:
ABAP Example Hints and Tips

Return to Index:-
SAP ABAP/4 Programming, Basis Administration, Configuration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.  The site www.gotothings.com is in no way affiliated with SAP AG.
Any unauthorised copying or mirroring is prohibited.