Professional Documents
Culture Documents
Objeto de Rederência Sap PDF
Objeto de Rederência Sap PDF
ABAP Objects
Reference
A comprehensive guide to all
ABAP-language elements
in two volumes
Volume I & II
Contents at a Glance
Volume I
Part 1
Syntax
29
Part 2
Program Structure
Part 3
Declarative Statements
Part 4
Generating Objects
Part 5
Part 6
Part 7
Assignments
Part 8
47
107
193
209
289
333
363
Volume II
Part 9
User Dialogs
503
Part 10
Part 11
Program Parameters
847
Part 12
Program Processing
863
Part 13
Part 14
Obsolete Statements
Part 15
Appendix
699
885
907
987
Contents
Preface
17
1.1
1.2
Described Releases
1.3
19
19
1.4
1.5
Book Structure
22
1.6
Search Options
25
1.7
Syntax Diagrams
Part 1 Syntax
19
19
20
26
29
ABAP Syntax
31
2.1
ABAP Statements
2.2
2.2.1
2.2.2
2.2.3
2.2.4
2.2.5
2.3
Naming Conventions
2.4
Chained Statements
2.5
Comments
31
39
43
44
45
47
Introductory Statements
49
3.1
Overview
3.2
3.2.1
3.2.2
3.2.3
Executable Programs 50
Additions for the Basic List of the Program 51
Addition for the Message Class 52
Addition for Defining a Logical Database 53
49
Contents
3.3
3.4
Function Groups
3.5
Class Pools
3.6
Interface Pools
3.7
Type Groups
Modularization Statements
4.1
Overview
4.2
4.2.1
4.2.2
4.2.3
Procedures 58
Methods 58
Function Modules
Subroutines 60
4.3
Dialog Modules
4.4
4.4.1
4.4.2
4.4.3
4.4.4
Event Blocks 66
Program Constructor 66
Reporting Events 67
Selection Screen Events 72
List Events 78
4.5
4.5.1
4.5.2
5.1
Overview
5.2
5.2.1
5.2.2
5.2.3
5.2.4
5.3
5.3.1
5.3.2
5.3.3
5.3.4
5.4
5.4.1
5.4.2
Contents
53
54
54
55
55
57
57
59
65
84
89
94
89
107
6.1
6.1.1
6.1.2
6.1.3
6.1.4
6.1.5
Overview 109
Validity and Visibility 109
Data Types 110
Data Objects 110
Absolute Type Names 112
Statements for Declaring Data Types and Data Objects
6.2
6.3
6.3.1
6.3.2
6.3.3
6.3.4
6.3.5
6.3.6
6.4
6.4.1
6.4.2
6.4.3
6.4.4
6.4.5
6.4.6
6.4.7
6.5
6.6
6.7
6.8
137
6.9
6.9.1
6.9.2
139
6.10
6.10.1
6.10.2
6.11
109
115
115
118
128
134
135
136
144
Contents
7.1
Overview
7.2
7.2.1
7.2.2
7.2.3
7.3
7.3.1
7.3.2
7.4
7.4.1
7.4.2
7.4.3
Typing
8.1
Overview
8.2
8.2.1
8.2.2
8.3
8.3.1
8.3.2
147
160
187
187
10
147
190
193
9.1
Overview
9.2
9.2.1
9.2.2
9.2.3
9.2.4
9.2.5
9.2.6
9.3
9.3.1
9.3.2
9.3.3
9.3.4
9.3.5
Contents
195
195
208
209
10
211
10.1
Overview
10.2
10.2.1
10.2.2
10.2.3
10.2.4
10.3
10.3.1
10.3.2
11
11.1
Overview
11.2
11.2.1
11.2.2
11.2.3
11.2.4
11.3
11.3.1
11.3.2
11.4
11.4.1
11.4.2
12
12.1
Overview
12.2
Exiting Programs
12.3
12.3.1
12.3.2
12.3.3
12.3.4
12.3.5
12.3.6
12.4
12.4.1
12.4.2
12.4.3
211
239
239
255
274
279
279
279
285
Contents
11
12
289
13
Logical Expressions
13.1
Overview
13.2
13.2.1
13.2.2
13.2.3
13.2.4
13.3
13.4
13.4.1
13.4.2
13.4.3
13.4.4
13.4.5
13.5
13.6
13.6.1
13.6.2
13.6.3
13.6.4
14
Control Structures
14.1
Overview
14.2
14.2.1
14.2.2
Branching 311
Branching with IF 312
Branching with CASE 313
14.3
14.3.1
14.3.2
Loops 314
Unconditional Loops with DO 314
Conditional Loops with WHILE 317
15
Exception Handling
15.1
15.1.1
15.1.2
15.1.3
Overview 319
Treatable Exceptions Prior to Release 6.10 319
Treatable Exceptions in Release 6.10 and Later 319
Statements for Exceptions 320
15.2
15.2.1
15.2.2
Contents
291
291
296
300
304
311
311
319
322
307
15.2.3
15.2.4
15.3
15.3.1
15.3.2
15.3.3
15.4
15.4.1
15.4.2
15.4.3
Part 7 Assignments
322
333
16
Value Assignments
335
16.1
Overview
16.2
16.2.1
16.2.2
16.2.3
16.3
16.4
Formatted Assignment
16.5
17
Setting References
17.1
Overview
17.2
17.2.1
17.2.2
17.2.3
17.3
17.4
356
18
359
18.1
Overview
18.2
18.2.1
18.2.2
18.3
18.4
Releasing Memory
335
336
338
339
341
343
343
343
356
359
361
362
Contents
13
14
363
19
Calculation Expressions
19.1
Overview
19.2
19.3
19.3.1
19.3.2
19.3.3
19.3.4
19.3.5
19.4
19.4.1
19.4.2
20
Calculation Statements
20.1
Overview
20.2
Addition
20.3
Subtraction
20.4
Multiplication
20.5
Division
21
21.1
21.1.1
21.1.2
21.1.3
21.1.4
Overview 377
Byte Strings and Character Strings 377
Statements for Processing Byte/Character Strings 377
Operands in Byte/Character String Processing 378
Treating Trailing Blanks During Character String Processing
21.2
21.3
21.3.1
21.3.2
381
21.4
21.4.1
21.4.2
388
21.5
21.5.1
21.5.2
21.5.3
21.6
Contents
365
365
365
370
373
373
373
374
374
375
377
396
380
395
379
21.7
398
21.8
399
21.9
401
22
407
22.1
Overview
22.2
22.2.1
22.2.2
22.2.3
22.2.4
22.3
22.3.1
22.3.2
22.3.3
22.4
22.4.1
22.4.2
22.4.3
22.5
Specifying Components
22.6
22.7
22.7.1
22.8
22.9
Editing Tables
23
23.1
Introduction
407
437
451
452
454
456
461
463
463
23.2
23.3
23.4
23.5
23.6
463
465
466
468
469
Contents
15
16
24
24.1
Overview
24.2
24.2.1
24.2.2
24.2.3
24.2.4
24.2.5
24.2.6
24.3
24.3.1
24.3.2
24.3.3
24.4
Contents
473
473
481
475
Contents
Part 9 User Dialogs
503
25
Dynpros
505
25.1
Overview
505
25.2
25.2.1
25.2.2
25.2.3
25.2.4
25.2.5
25.2.6
25.3
25.3.1
25.3.2
25.3.3
25.3.4
25.3.5
25.3.6
25.3.7
25.3.8
25.3.9
25.3.10
25.3.11
25.3.12
25.3.13
25.3.14
25.3.15
26
Selection Screens
26.1
26.1.1
26.1.2
26.1.3
26.1.4
26.1.5
26.1.6
Overview 547
Selection Screens as Dynpros 547
Selection Screen Tasks 547
GUI Status of Selection Screens 547
Selection Screen Events 548
Selection Screens and Logical Databases
Statements for Selection Screens 548
26.2
26.2.1
26.2.2
26.2.3
26.2.4
547
548
570
Contents
491
492
26.3
26.3.1
26.3.2
26.3.3
26.3.4
26.4
26.4.1
26.4.2
26.4.3
26.4.4
26.4.5
26.5
26.5.1
26.5.2
26.5.3
26.5.4
26.5.5
27
Lists
27.1
27.1.1
27.1.2
27.1.3
27.1.4
27.1.5
27.1.6
27.1.7
27.1.8
27.1.9
Overview 605
Lists as Screens 605
Lists in the ABAP Program 605
Basic List 605
Details Lists 605
The Structure of a List 606
Print Lists 607
Lists and ABAP Objects 607
Lists and Unicode 607
Statements for List Processing 608
27.2
27.2.1
27.2.2
27.2.3
27.2.4
27.2.5
27.2.6
27.2.7
27.2.8
27.2.9
27.2.10
27.2.11
27.2.12
27.3
27.3.1
27.3.2
Contents
605
665
593
27.3.3
27.3.4
27.4
27.5
27.5.1
27.5.2
27.5.3
27.5.4
27.6
27.6.1
27.6.2
28
Messages
28.1
28.1.1
28.1.2
Overview 689
Storing Messages 689
Message Types 689
28.2
28.2.1
28.2.2
28.2.3
28.2.4
28.2.5
673
676
678
689
699
29
Open SQL
701
29.1
29.1.1
29.1.2
29.1.3
29.1.4
29.1.5
29.1.6
29.1.7
Overview 701
Scope of Open SQL 701
Database Interface 701
Database Access 701
Client Handling 701
SAP Buffering 701
LUW 702
Open SQL Statements 702
29.2
29.2.1
29.2.2
29.2.3
29.2.4
29.2.5
29.2.6
29.2.7
Contents
493
494
29.3
29.3.1
29.3.2
29.3.3
29.4
29.4.1
29.4.2
29.5
29.5.1
29.5.2
29.6
29.6.1
29.6.2
29.7
29.7.1
29.7.2
29.8
29.9
30
Native SQL
739
743
754
760
761
763
30.1
Overview
30.2
30.2.1
30.2.2
30.2.3
30.2.4
30.2.5
30.3
30.4
31
Data Clusters
31.1
Overview
31.2
31.2.1
31.2.2
31.2.3
31.2.4
31.3
31.3.1
31.3.2
31.3.3
Contents
763
769
773
774
775
775
783
782
795
31.3.4
31.4
31.5
31.6
32
32.1
32.1.1
32.1.2
32.1.3
32.1.4
32.1.5
32.1.6
Overview 799
Addressing Files 799
Authorizations for Accessing Files 799
Locks 801
The File Interface in Unicode Programs 801
File Size 803
File Interface Statements 803
32.2
32.2.1
32.2.2
32.2.3
32.2.4
32.2.5
32.2.6
32.3
32.3.1
32.3.2
32.3.3
32.3.4
32.4
32.4.1
32.4.2
32.4.3
32.4.4
32.4.5
32.5
32.5.1
32.5.2
32.5.3
32.6
32.6.1
32.6.2
32.6.3
32.7
Closing a File
32.8
Deleting a File
795
797
798
799
818
827
831
832
833
Contents
495
33
Data Consistency
835
33.1
Overview
33.2
33.2.1
33.2.2
33.3
33.3.1
33.3.2
33.3.3
33.4
Database Locks
33.5
33.5.1
33.5.2
33.6
Authorization Check
835
842
844
496
847
849
34.1
Overview
34.2
Setting Parameters
849
34.3
Reading Parameters
35
Language Environment
35.1
Overview
35.2
849
850
851
851
35.3
35.4
35.5
36
Time Stamp
36.1
Overview
36.2
36.3
36.3.1
36.3.2
36.3.3
36.4
Current Time
Contents
852
854
855
857
857
861
858
859
851
863
37
37.1
Overview
865
865
37.2
Setting Breakpoints
37.3
37.3.1
37.3.2
37.3.3
865
868
37.4
869
38
871
38.1
Overview
38.2
38.3
38.4
Checking Syntax
38.5
38.6
881
38.7
882
38.8
871
872
875
876
878
883
885
39
887
39.1
39.1.1
39.1.2
39.1.3
39.1.4
39.1.5
39.1.6
39.1.7
Overview 887
Introduction 887
Destination 888
System Fields in RFC 889
Exceptions in RFCs 889
RFC Authorization 889
Inadmissible Actions 890
RFC Interface Statements 890
39.2
39.2.1
39.2.2
39.2.3
Contents
497
498
40
XSLT Transformations
901
40.1
Overview
40.2
40.2.1
40.2.2
40.2.3
40.2.4
Call 901
Specifying the Transformation 902
Transformation Source 902
Transformation Result 903
Explanation of Addition Options 903
901
907
41
Obsolete Statements
909
41.1
Overview
41.2
Obsolete Syntax
41.3
41.3.1
41.4
41.4.1
41.4.2
41.4.3
41.4.4
41.5
41.6
41.7
41.8
41.8.1
41.8.2
41.9
41.9.1
41.9.2
41.9.3
41.10
41.10.1
41.10.2
41.10.3
41.10.4
41.10.5
41.10.6
41.11
41.11.1
41.11.2
41.11.3
Contents
909
909
918
920
922
923
936
41.12
41.12.1
41.12.2
41.12.3
41.13
41.13.1
41.13.2
41.13.3
41.13.4
Contexts 949
Overview 949
Creating Instances of Contexts 950
Providing Contexts with Key Values 950
Querying Contexts 951
960
Part 15 Appendix
953
967
970
987
A.1
Overview
989
A.2
A.2.1
A.2.2
A.2.3
A.2.4
A.2.5
A.2.6
A.2.7
A.2.8
A.2.9
A.2.10
A.2.11
A.3
A.3.1
A.3.2
A.4
989
1006
1007
Contents
499
500
A.5
A.5.1
A.5.2
A.5.3
A.5.4
B.1
Auxiliary Classes
B.2
B.3
B.4
B.5
B.6
B.7
B.8
B.9
B.10
B.11
Object Services
B.12
JavaScript Integration
C.1
C.1.1
C.1.2
C.2
C.3
1013
1013
1013
1013
1014
1014
1014
1015
1015
1016
1016
1016
1017
1019
1019
1022
1022
1025
D.1
Overview
D.2
D.3
D.3.1
D.3.2
D.3.3
D.3.4
D.3.5
D.3.6
D.3.7
D.3.8
Contents
1025
1025
D.3.9
Exception Group for Errors in the Language Environment 1033
D.3.10 Exception Group for Errors with Remote Function Calls 1033
D.3.11 Catchable Runtime Errors That Are Not Assigned to Any Group 1033
Glossary
Index
1035
1037
1079
Contents
501
7.1
Overview
define one global class of the class library. This global class can be used
in all other ABAP programs. You can also define local classes and interfaces in a class pool, for use in the class pool itself.
In an interface pool, you can use the Class Builder in the ABAP Work-
bench to define one global interface of the class library. This global
interface can be used in all other ABAP programs. You cannot define
local classes or interfaces in an interface pool.
In all other ABAP programs (except type groups), you can define local
Section
CLASS
7.2
INTERFACE
7.3
METHODS, CLASS-METHODS
7.4.1
EVENTS, CLASS-EVENTS
7.4.2
INTERFACES, ALIASES
7.4.3
7.2
Defining Classes
CLASS
The full definition of a class consists of a declaration section and an implementation section, both of which you introduce with CLASS. You define
147
7.2.1
Declaration Section
Visibility Areas
Syntax
PUBLIC SECTION.
This statement defines the public visibility area of class class. All class
components that you declare in the area after the PUBLIC SECTION
statement can be addressed from outside the class, from within its subclasses, and within the class itself.
148
Syntax
PROTECTED SECTION.
This statement defines the protected visibility area of class class. All
class components that you declare in the area after the PROTECTED SECTION statement can be addressed in the subclasses of the class and within
the class itself.
Private visibility area
Syntax
PRIVATE SECTION.
This statement defines the private visibility area of class class. All class
components that you declare in the area after the PRIVATE SECTION
statement can be addressed only within the class itself.
7.2.1.2
Class Components
Syntax
components.
You define the components of the classes in the visibility areas. The following declaration statements are possible for components:
Note
All the components of a class lie within one namespace. The name of a
component must be unique within a class, regardless of its type (data
type, attribute, method, event, or alias name). The components of an
implemented interface differ from the directly declared components of
the class through prefix ifac~ (name of the interface with the interface
component selector).
Defining Classes
149
7.2.1.3
Global Classes
Syntax
... PUBLIC ...
When you use the addition PUBLIC, the class becomes a global class of
the class library. The addition PUBLIC is possible for only one class of a
class pool and is generated by the Class Builder when you create a global
class. All classes without the addition PUBLIC are local classes of their
program.
Note
You cannot use the TYPES statement to declare separate data types in the
public visibility area of a global class. Moreover, you can use only globally
declared data types there.
7.2.1.4
Defining Subclasses
Syntax
... INHERITING FROM superclass ...
If you use the addition INHERITING FROM, the class is derived from the
superclass superclass through inheritance, and therefore becomes its
direct subclass. The superclass superclass can be any non-final class that
is visible at this point.
Each class can have only one direct superclass, but can have several direct
subclasses (simple inheritance). Any class without the addition INHERITING FROM implicitly inherits from the predefined empty class object.
Taken together, all the classes in ABAP Objects form an inheritance tree, in
which a clearly defined path exists from each class to the root node,
object.
The class class inherits the components of its superclass superclass
without changing their visibility area. Only the components of the public
and protected visibility areas of the superclass are visible in the subclass.
The attributes of the inherited components cannot be changed. You can
also declare additional components in a subclass and redefine inherited
methodsthat is, you can re-implement them without changing the
interface.
150
Note
The public and protected components of all the classes within a path of
the inheritance tree all lie within the same namespace. New components
in a subclass may not have names that are identical to public or protected
components that were inherited from the superclasses.
7.2.1.5
Abstract Classes
Syntax
... ABSTRACT ...
The addition ABSTRACT defines an abstract class, class. You cannot generate instances of an abstract class. To use the instance components of an
abstract class, you have to instantiate a non-abstract subclass of the class.
7.2.1.6
Final Classes
Syntax
... FINAL ...
The addition FINAL defines a final class class. You cannot derive subclasses of a final class. All methods of a final class are implicitly final, which
means that you cannot explicitly declare them as final.
Note
If a class is both abstract and final, you can use only its static components.
While you can declare instance components, you cannot use them.
Example
In the example below, an abstract class c1 and a final class c2 are defined,
and c2 inherits from c1. c1 is implicitly a subclass of the blank class
object. You can access m1 in c2, but a1.
Defining Classes
151
METHODS m2.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD m1.
WRITE / a1.
ENDMETHOD.
ENDCLASS.
CLASS c2 IMPLEMENTATION.
METHOD m2.
m1( ).
ENDMETHOD.
ENDCLASS.
...
DATA oref TYPE REF TO c2.
CREATE OBJECT oref.
oref->m2( ).
7.2.1.7
Instantiation
Syntax
... CREATE {PUBLIC|PROTECTED|PRIVATE} ...
The addition CREATE defines the context in which class can be instantiatedthat is, where the CREATE OBJECT statement can be executed for
that class.
A class with the addition
152
CREATE additions. Direct subclasses that are friends of the class implicitly inherit the addition CREATE PRIVATE. You can explicitly specify all
CREATE additions for friends of superclasses that can be instantiated as
private to override the inherited one.
Note
If a class can be instantiated as private, we recommend making it final as
well, as its subclasses cannot be instantiated if they are not friends of the
class.
7.2.1.8
Friends
Syntax
... [GLOBAL] FRIENDS [classi] [ifaci].
The addition FRIENDS makes class class a friend of classes classi and/
or interfaces ifaci. At the same time, all subclasses of classes classi, all
classes that implement one of the interfaces ifaci, and all interfaces that
have one of the interfaces ifaci as a component interface become friends
of class. You have to specify at least one class or interface.
The friends of a class have unrestricted access to the protected and private
components of that class, and can generate unrestricted instances of the
class.
The friends of class are not automatically friends of subclasses of class.
Moreover, the addition FRIENDS does not make class a friend of its
friends.
If you do not use the GLOBAL additions, you can specify all classes and
interfaces for classi and ifaci that are visible at this point. If you define
global classes and interfaces in the class library as friends, please note that
in these objects the local classes of other ABAP programs are not visible.
In such cases, static access to the components of class is not possible.
The addition GLOBAL is allowed only when you use the addition PUBLIC
for the global class of a class poolat the same time. You can list other
Defining Classes
153
global classes and interfaces from the class library after GLOBAL FRIENDS.
This addition is generated when the Class Builder creates a global class
and you have specified friends at the corresponding tab page of the Class
Builder.
Example
In the example below, class c2 is a friend of interface i1, and therefore
also of the implementing class c1. It can instantiate these objects and
access their private component a1.
INTERFACE i1.
...
ENDINTERFACE.
CLASS c1 DEFINITION CREATE PRIVATE FRIENDS i1.
PRIVATE SECTION.
DATA a1(10) TYPE c VALUE 'Class 1'.
ENDCLASS.
CLASS c2 DEFINITION.
PUBLIC SECTION.
INTERFACES i1.
METHODS m2.
ENDCLASS.
CLASS c2 IMPLEMENTATION.
METHOD m2.
DATA oref TYPE REF TO c1.
CREATE OBJECT oref.
WRITE oref->a1.
ENDMETHOD.
ENDCLASS.
Local friends of global classes
This statement turns the local classes and interfaces of a class pool,
classi and ifaci, into friends of its global class. You have to specify at
least one class or interface.
154
This section does not introduce a declaration section, and you cannot
complete it with ENDCLASS. You have to specify it in the class pool before
the definition of the local classes and interfaces, classi and ifaci, if you
want them to have access to the private and protected components of the
global class, class.
7.2.2
Implementation Section
ration segment of the class with the statement METHODS ... REDEF-
INITION
The implementation of each method corresponds to a METHOD ENDMETHOD processing block (see Section 4.2.1). No statements are allowed
outside of method implementations in the implementation section.
Within a method implementation, you can access all the components in
instance methods and all static components of their own class in static
methods. The component selector is not necessary to address components in their own class. An implicitly generated local reference variable
called me is available in the implementation of every instance method. me
points to the current instance of the method.
Note
A class thatin accordance with its declaration sectiondoes not have
to implement any methods has either a blank implementation section or
none at all.
Defining Classes
155
Example
In the following example, three methods of class c2 have to be implemented. Method m1 in c1 is abstract and does not have to be implemented.
INTERFACE i1.
METHODS m1.
ENDINTERFACE.
CLASS c1 DEFINITION ABSTRACT.
PROTECTED SECTION.
METHODS m1 ABSTRACT.
ENDCLASS.
CLASS c2 DEFINITION INHERITING FROM c1.
PUBLIC SECTION.
INTERFACES i1.
METHODS m2.
PROTECTED SECTION.
METHODS m1 REDEFINITION.
ENDCLASS.
CLASS c2 IMPLEMENTATION.
METHOD m1.
...
ENDMETHOD.
METHOD m2.
...
ENDMETHOD.
METHOD i1~m1.
...
ENDMETHOD.
ENDCLASS.
7.2.3
Declaring Classes
156
The two variants of the CLASS statement serve to declare a class class in
the program independently of the location of the actual definition. They
do not introduce a declaration section, and you cannot complete them
with ENDCLASS. The additions have the following meanings:
The variant with addition
Defining Classes
157
7.3
Defining Interfaces
INTERFACE
7.3.1
Declaration Section
Interface Components
Syntax
components
Note
All the components of an interface lie within one namespace. The name
of a component must be unique within an interface, regardless of its type
(data type, attribute, method, event, or alias name). The components of
an integrated interface differ from the directly declared components
through prefix ifac~ (name of the interface with the interface component selector).
158
Example
In the following example, an interface, i1, is declared with three interface
components, a1, m1, and e1. Class c1 implements the interface, which
makes the interface components public components of the class that can
be addressed using the interface component selector (~).
INTERFACE i1.
DATA
a1 TYPE string.
METHODS m1.
EVENTS e1 EXPORTING value(p1) TYPE string.
ENDINTERFACE.
CLASS c1 DEFINITION.
PUBLIC SECTION.
INTERFACES i1.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD i1~m1.
RAISE EVENT i1~e1 EXPORTING p1 = i1~a1.
ENDMETHOD.
ENDCLASS.
7.3.2
Declaring Interfaces
Defining Interfaces
159
7.4
You use the declaration section of classes and interfaces to declare the
components of classes and interface that are major components of a
class:
Attributes
Methods
Events
Attributes are the data objects of a class. You can also declare class-specific data types. You declare attributes and data types with the general
statements TYPE-POOLS, TYPES, DATA, CLASS-DATA, and CONSTANTS,
whichwith the exception of CLASS-DATAcan also be used in other
contexts and are described in Chapter 6.
To declare methods and events, you use specific statements that are possible only in the declaration section of classes and interfaces.
7.4.1
Methods
You use the METHODS and CLASS-METHODS statements to declare methods. They define how the class responds. You define the interface of a
method in the declaration. Different categories of methods exist for specific tasks:
General methods
Functional methods
Constructors
Event handlers
Instance Methods
METHODS
160
7.4.1.2
The most general form of the METHODS statement lets you define instance
methods with any input and output parameters.
Syntax
METHODS meth [ABSTRACT|FINAL]
[IMPORTING {parameteri} [PREFERRED PARAMETER p]]
[EXPORTING {parameteri}]
[CHANGING {parameteri}]
[{RAISING|EXCEPTIONS} {exci}].
The additions IMPORTING, EXPORTING, and CHANGING define the signature of method meth:
You define the corresponding formal parameters by specify a list, parameteri, after each addition.
161
Syntax of parameteri
... { {VALUE(pi)}|{REFERENCE(pi)}|{pi} {typing}
[OPTIONAL|{DEFAULT defi}] } ...
162
Syntax
... RAISING {exci} ...
You can use the addition RAISING to pass class-based exceptions, exci,
on to the calling program. The exceptions can be subclasses of CX_
STATIC_CHECK and CX_DYNAMIC_CHECK which are triggered in or
propagated into the method of the ABAP runtime environment or with
the RAISE EXCEPTION statement, but are not handled in a TRY block
(see Section 15.2).
exci can equal any exception classes that are subclasses of the above
superclasses and are visible at this point. You have to enter the exception
classes in the ascending order of their inheritance hierarchy.
If an exception of a superclass occurs in the method and is neither handled nor passed on, this will cause either a syntax error or an exception
that the calling program has to handle, CX_SY_NO_HANDLER
Notes
Exceptions that involve the subclasses of CX_STATIC_CHECK and CX_
Example
In class math below, method divide_1_by propagates all the exceptions
represented by class CX_SY_ARITHMETIC_ERROR and its subclasses. If an
input parameter, operand, is given a 0 value during a call, for example,
163
exception CX_SY_ZERODIVIDE is triggered, propagated, andas illustrated in the examplehandled by the calling program in a TRY block.
Syntax
... EXCEPTIONS {exci} ...
164
165
Syntax
... ABSTRACT ...
Syntax
METHODS ... FINAL ...
The addition FINAL is possible only in classes, not in interfaces. The addition FINAL defines a final method, meth. A final method cannot be redefined in a subclass. All methods in final classes are automatically final, and
the addition FINAL is not allowed here.
7.4.1.3
Functional methods can have any number of input parameters, and have
exactly one return value as output parameter.
166
Syntax
METHODS meth [ABSTRACT|FINAL]
[IMPORTING {parameteri}]
RETURNING VALUE(r) {TYPE type_spec}|{LIKE dobj_spec}
[{RAISING|EXCEPTIONS} {exci}].
This statement declares a functional instance method, meth. The additions ABSTRACT, FINAL, IMPORTING, RAISING, and EXCEPTIONS have
the same functions as for general instance methods.
Instead of the additions EXPORTING and CHANGING, a functional method
has one addition, RETURNING, that defines exactly one formal parameter,
r, as a return value. The return value must be passed on with VALUE.
As far as the typing of return value r with typing is concerned, the same
rules apply as for the typing of IMPORTING, EXPORTING, and CHANGING
parameters (see Section 8.2), with the exception that a return value
always has to be completely typed: You cannot specify any of the generic
types from Table 5.3 for typing.
You can use functional methods in the operand positions of statements
where a data object is expected, as described in Section 2.2.5. The complete typing of the return value defines the data type of the operand.
When you execute statements of this type, the functional method is
called, and the content of the return value replaces the content of the
operand.
Notes
When you use a functional method in an operand position, class-based
167
Instance Constructors
Instance constructors are methods with the predefined name constructor that are called automatically during the instantiation of their class.
Constructors can have any number of input parameters, but no output
parameters.
Syntax
METHODS constructor [FINAL]
[IMPORTING {parameteri}]
[{RAISING|EXCEPTIONS} {exci}].
This statement is possible only in the public visibility area of the declaration section of a class. It declares the instance constructor of the class,
constructor.
168
Each class has a predefined method called constructor in its public visibility area. You can use the explicit declaration to define the interface of
method constructor class-specifically and to implement its functionality. If you do not declare it explicitly, the instance constructor assumes the
signature from the instance constructor in the direct superclass and calls
it implicitly.
If you implement the instance constructor in a subclass, you have to call
the instance constructor of the superclass explicitly (with the pseudo reference super->constructor) and supply its interface with data. The
only exceptions to this are the direct subclasses of the root node, object.
Prior to the call of the superclass constructor, an instance constructor can
access only the static components of its class. After the superclass constructor is called, it can also access the instance constructors.
You can use the addition IMPORTING to define input parameters, according to the same rules as for the general methods. The additions RAISING
and EXCEPTIONS for propagating and defining exceptions also have the
same meaning as under the general methods.
The instance constructor is called once for each instance of a class, with
the CREATE OBJECT statement, directly after the instance is generated. In
the process, actual parameters must be assigned for all non-optional input
parameters, and exceptions can be handled or passed on. A call using
CALL METHOD is not possible, except in the call of the superclass constructor with super->constructor in the redefined constructor of a
subclass.
Instance constructors are implicitly final. You can specify the addition
FINAL, but it is not necessary.
Notes
Instance constructors represent an exception to the rule that all public
class for technical reasons. You control the actual visibility with the
addition CREATE {PUBLIC|PROTECTED|PRIVATE} of the CLASS
DEFINITION statement.
169
Example
In the following example, class c2 inherits from class c1. The instance
constructor, constructor, is declared explicitly in both classes. It therefore has to be implemented in both classes. In addition, the implementation in c2 has to contain the call of the superclass constructor.
CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS constructor IMPORTING p1 TYPE any.
...
ENDCLASS.
CLASS c2 DEFINITION INHERITING FROM c1.
PUBLIC SECTION.
METHODS constructor IMPORTING p2 TYPE any.
...
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD constructor.
...
ENDMETHOD.
ENDCLASS.
CLASS c2 IMPLEMENTATION.
METHOD constructor.
...
super->constructor( p2 ).
...
ENDMETHOD.
ENDCLASS.
7.4.1.5
Event Handlers
Event handlers are methods that can also be called with CALL METHOD,
but which are called primarily by triggering an event of a class or an interface. The only possible formal parameters of an event handler are the
input parameters that are defined as the output parameters of the event.
Syntax
METHODS meth [ABSTRACT|FINAL]
FOR EVENT evt OF {class|ifac}
[IMPORTING {pi} [sender]].
170
This statement declares the instance method meth as the event handler
evt of class class or interface ifac class. ifac can be any classes or
interfaces that are visible at this point and that contain a visible event evt
as a component.
If event evt is an instance event, event handler meth can handle it for all
objects whose class is class or a subclass of class, or that implement
interface ifac directly or through a superclass. If the event is a static
event, event handler meth can handle it for class and its subclasses, or
for all classes that implement interface ifac.
You can use the additions ABSTRACT and FINAL to make the event handler abstract or final, just like the general methods.
The addition IMPORTING defines the input parameters of the event handler. pi can only be names of formal parameters that you defined as
export parameters of the event with addition EXPORTING for statement
[CLASS-]EVENTS in the declaration of event evt in the class or interface ifac. The additions TYPE/LIKE and OPTIONAL/DEFAULT are not
possible. Any default parameters, the typing of the input parameters, and
whether or not the input parameters are optional are taken from the declaration of the event. You do not have to specify all the output parameters
of the event.
If evt is an instance event, you can also define a formal parameter called
sender as an input parameter of the event handler, in addition to its
explicitly defined output parameters. sender is an implicit output parameter of every instance event. It is fully typed as a reference variable that
has class or interface ifac as a static type, as defined in the declaration
of the event handler after EVENT evt OF. If the event handler is called by
an instance event, sender passes on a reference to the triggering object.
Prior to Release 6.10, the static type of formal parameter sender was
determined by the class or interface in which the EVENTS statement
declared the event. In Release 6.10 and later, the event handler determines the type of its formal parameter itself.
Example
In the example below, class picture contains an event handler handle_
double_click for instance event picture_dblclick of global class
cl_gui_picture. The event handler uses two explicit output parameters of the event, along with the implicit parameter sender, as input
parameters.
171
A method declared in a superclass can be redefined in a subclass, provided it is not flagged as final in the superclass. Redefinition does not
change the methods interface.
Syntax
METHODS meth [FINAL] REDEFINITION.
172
during redefinition, the method is final from the current class and can no
longer be redefined in its subclasses.
Note
Each object reference that points to an object in a subclass addresses the
redefined methods regardless of its static type. This also applies to the
self-reference me->.
Example
In the example below, method m1 of superclass c1 is redefined in subclass
c2. The original implementation is called with super->m1. Both methods
use the private attribute a1 of the respective class. The redefined method
is executed with reference variable oref, which has static type c1 and
dynamic type c2.
CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS m1 IMPORTING p1 TYPE string.
PRIVATE SECTION.
DATA a1 TYPE string VALUE `c1: `.
ENDCLASS.
CLASS c2 DEFINITION INHERITING FROM c1.
PUBLIC SECTION.
METHODS m1 REDEFINITION.
PRIVATE SECTION.
DATA a1 TYPE string VALUE `c2: `.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD m1.
CONCATENATE a1 p1 INTO a1.
WRITE / a1.
ENDMETHOD.
ENDCLASS.
CLASS c2 IMPLEMENTATION.
METHOD m1.
super->m1( p1 ).
CONCATENATE a1 p1 INTO a1.
WRITE / a1.
ENDMETHOD.
ENDCLASS.
173
...
DATA oref TYPE REF TO c1.
CREATE OBJECT oref TYPE c2.
oref->m1( `...` ).
7.4.1.7
Static Methods
CLASS-METHODS
The most general form of the CLASS-METHODS statement lets you define
static methods with any number of input and output parameters.
Syntax
CLASS-METHODS meth
[IMPORTING {parameteri} [PREFERRED PARAMETER p]]
[EXPORTING {parameteri}]
[CHANGING {parameteri}]
[{RAISING|EXCEPTIONS} {exci}].
Functional methods can have any number of input parameters, and have
exactly one return value as output parameter.
174
Syntax
CLASS-METHODS meth
[IMPORTING {parameteri}]
RETURNING VALUE(r) {TYPE type_spec}|{LIKE dobj_spec}
[{RAISING|EXCEPTIONS} {exci}].
175
This statement is possible only in the public visibility area of the declaration section of a class. It declares the static constructor of the class,
class_constructor.
Each class has a predefined method called class_constructor in its
public visibility area. The explicit declaration enables you to implement its
functionality class-specifically. If you do not declare it explicitly, the static
constructor is blank.
The static constructor is executed once for each class and internal session.
This execution takes place automatically before the first access to the
class. You access a class whenever you generate an instance of that class
or address a static component with the class component selector.
During the first access of a subclass, the system searches for the nexthighest superclass in the inheritance tree whose static constructor has not
been executed yet. The static constructor of the superclass is then executed, followed successively by each subsequent subclass down to the
addressed subclass.
Like all static methods, the static constructor can access only the static
components of its class. Furthermore, the static constructor cannot
address its own class explicitly.
Notes
Like instance constructors, static constructors represent another excep-
tion to the rule that all public components along a path in the inheritance tree have to line in the same namespace.
The execution time for the static constructor is indeterminate. The sys-
tem merely guarantees its execution prior to the first access of the
class. The static constructor is usually executed immediately prior to
the access of the class. The static constructor is called at the start of the
respective processing block only if a static attribute of the class is
accessed. We do not recommend writing programs that require the
static constructor to be executed at a specific time.
176
Example
The static constructor of the class below sets static attribute access_
program to the name of the program of the first internal session that uses
a class during the first access to that class.
Static event handlers are static methods that are called by an event of a
class or an interface. The only possible formal parameters of an event handler are input parameters that are defined as output parameters of the
event.
CLASS-METHODS meth
FOR EVENT evt OF {class|ifac}
[IMPORTING {pi} [sender]].
This statement declares the static method meth as the event handler evt
of class or interface ifac. The syntax and meaning of the additions are
identical to those in the declaration of instance methods as event handlers.
Static event handlers can be called by event evt independently of an
instance of a class.
Example
In the class described below, dialog_box, a static event handler close_
box is defined for the event. This event is triggered when the user
attempts to close a dialog box of the GUI control framework (CFW).
177
Events
Instance Events
EVENTS
The EVENTS statement declares instance events. Instance events are tied
to objects. They can be triggered only in instance methods of the same
class.
178
Syntax
EVENTS evt [EXPORTING { VALUE(pi) typing
[ OPTIONAL|{DEFAULT defi}] } ].
179
Example
In the interface below, window, three events are declared, each with an
explicit, non-optional output parameter, status. Class dialog_window
implements interface window. Interface window_handler contains event
handlers that import both the explicit parameter, status, and the
implicit parameter, sender. The static type of input parameter sender is
class dialog_window. Prior to Release 6.10, interface window is the static
type.
INTERFACE window.
EVENTS: minimize EXPORTING VALUE(status) TYPE i,
maximize EXPORTING VALUE(status) TYPE i,
restore EXPORTING VALUE(status) TYPE i.
ENDINTERFACE.
CLASS dialog_window DEFINITION.
PUBLIC SECTION.
INTERFACES window.
ENDCLASS.
INTERFACE window_handler.
METHODS: minimize_window
FOR EVENT window~minimize OF dialog_window
IMPORTING status sender,
maximize_window
FOR EVENT window~maximize OF dialog_window
IMPORTING status sender,
restore
FOR EVENT window~restore OF dialog_window
IMPORTING status sender.
ENDINTERFACE.
7.4.2.2
Static Events
CLASS-EVENTS
The CLASS-EVENTS statement declares static events. Static events are not
tied to objects. They can be triggered in all methods of the same class.
Syntax
CLASS-EVENTS evt [EXPORTING { VALUE(pi) typing
[ OPTIONAL|{DEFAULT defi}] } ].
180
7.4.3
Interfaces can be implemented by classes or integrated by other interfaces. You can define alias names for interface components.
INTERFACES
Syntax
INTERFACES ifac
{ { [ABSTRACT METHODS {methi}]
[FINAL METHODS {methj}] }
| [ALL METHODS {ABSTRACT|FINAL}] }
[DATA VALUES {attri = vali}].
In the public visibility area of a class, the INTERFACES statement implements interface ifac in the class. You can also specify additions, which
define the properties of interface components in the class.
ifac can be any local or global interfaces that are visible at this point. The
implementation makes the components of the interfaces public components of the class. An interface component named comp has the name
ifac~comp in the class: ifac is the name of the interface and the ~ character is the interface component selector. A class has to implement all
methods of the interface in its implementation section, provided it is not
declared as abstract.
181
You can use the additions ABSTRACT METHODS and FINAL METHODS to
make individual instance methods of the interface, methi, abstract or final
in the implementing class. The same rules apply as for the additions
ABSTRACT and FINAL of the METHODS statement. In particular, when you
make an interface method abstract, the entire class has to be abstract,
and you cannot list an interface method after both ABSTRACT METHODS
and FINAL METHODS at the same time. Instead of making individual
instance methods abstract or final in the class, you can use the addition
ALL METHODS {ABSTRACT|FINAL} to make all the interface methods
either abstract or final.
You can use the addition DATA VALUES to assign initial values to the individual attributes, attri. This addition has the same function for instance
attributes as the addition VALUE of the DATA statement for internal class
attributes. If you declare a constant with the CONSTANTS statement in the
interface, you cannot list it after the addition DATA VALUES.
Notes
A class can implement any number of different interfaces. All the interfaces implemented in a class are equally valid on one level. If an interface
implemented in a class, ifac, is a compositethat is, it contains component interfacesthese interfaces are implemented as individual interfaces
in the class, regardless of their nesting hierarchy. As a result, their components are not addressed through the name ifac, but instead through the
name of the component interfaces. Multiple use of the interface component selector in an identifier (such as ifac1~ifac2~comp) is generally not
supported.
Each interface exists only once in a class, and each interface component
comp can be uniquely addressed with the name ifac~comp. Even a component of an interface that is seemingly implemented several times in a
classbecause it is an interface component of one or more other interfaces, for examplereally exists only once.
7.4.3.2
Syntax
INTERFACES ifac.
Within the declaration of an interface, the INTERFACES statement integrates interface ifac in the declared interface. You cannot specify any
additions. As a result, interface ifac becomes a component interface of a
composite interface.
182
INTERFACE i1.
METHODS m1.
ENDINTERFACE.
183
INTERFACE i2.
INTERFACES i1.
METHODS m2.
ENDINTERFACE.
INTERFACE i3.
INTERFACES i1.
METHODS m3.
ENDINTERFACE.
CLASS c1 DEFINITION.
PUBLIC SECTION.
INTERFACES: i2, i3.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD i1~m1.
...
ENDMETHOD.
METHOD i2~m2.
...
ENDMETHOD.
METHOD i3~m3.
...
ENDMETHOD.
ENDCLASS.
...
DATA iref1 TYPE REF TO i1.
CREATE OBJECT iref1 TYPE c1.
iref1->m1( ).
7.4.3.3
Alias Names
ALIASES
184
INTERFACE i1.
METHODS meth.
ENDINTERFACE.
INTERFACE i2.
INTERFACES i1.
ALIASES m1 FOR i1~meth.
METHODS meth.
ENDINTERFACE.
INTERFACE i3.
INTERFACES i2.
ALIASES: m1 FOR i2~m1,
m2 FOR i2~meth.
METHODS meth.
ENDINTERFACE.
CLASS c1 DEFINITION.
PUBLIC SECTION.
INTERFACES i3.
ALIASES: m1 FOR i3~m1,
185
m2 FOR i3~m2,
m3 FOR i3~meth.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD i1~meth.
... m2( ) ...
ENDMETHOD.
METHOD i2~meth.
... m3( ) ...
ENDMETHOD.
METHOD i3~meth.
... m1( ) ....
ENDMETHOD.
ENDCLASS.
186