ABAP Objects Reference

Embed Size (px)

Citation preview

  • Horst Keller, Joachim Jacobitz

    ABAP Objects Reference

    A comprehensive guide to all

    ABAP-language elements

    in two volumes

    Volume I & II

    titel.fm Seite 3 Freitag, 31. Januar 2003 2:28 14

  • Content at the Glance 5

    Contents at a Glance

    Volume I

    Part 1 Syntax 29

    Part 2 Program Structure 47

    Part 3 Declarative Statements 107

    Part 4 Generating Objects 193

    Part 5 Calling and Exiting Program Units 209

    Part 6 Program Flow Control 289

    Part 7 Assignments 333

    Part 8 Processing Internal Data 363

    Volume II

    Part 9 User Dialogs 503

    Part 10 Processing External Data 699

    Part 11 Program Parameters 847

    Part 12 Program Processing 863

    Part 13 External Programming Interfaces 885

    Part 14 Obsolete Statements 907

    Part 15 Appendix 987

    abapEAL.fm Seite 5 Freitag, 31. Januar 2003 12:51 12

  • Contents 7

    Contents

    Preface 17

    1 Introduction and Overview 19

    1.1 Purpose and Objective 19

    1.2 Described Releases 19

    1.3 SAP Basis and Web Application Server 19

    1.4 ABAP and Unicode 20

    1.5 Book Structure 22

    1.6 Search Options 25

    1.7 Syntax Diagrams 26

    Part 1 Syntax 29

    2 ABAP Syntax 31

    2.1 ABAP Statements 31

    2.2 ABAP Language Elements 312.2.1 Operators 322.2.2 Operands 332.2.3 Designations for Operands 332.2.4 Specifying Operands Statically or Dynamically 392.2.5 Data Objects in Operand Positions 40

    2.3 Naming Conventions 43

    2.4 Chained Statements 44

    2.5 Comments 45

    Part 2 Program Structure 47

    3 Introductory Statements 49

    3.1 Overview 49

    3.2 Executable Programs 503.2.1 Additions for the Basic List of the Program 513.2.2 Addition for the Message Class 523.2.3 Addition for Defining a Logical Database 53

    abapIVZ.fm Seite 7 Freitag, 31. Januar 2003 12:51 12

  • 8 Contents

    3.3 Module Pools and Subroutine Pools 53

    3.4 Function Groups 54

    3.5 Class Pools 54

    3.6 Interface Pools 55

    3.7 Type Groups 55

    4 Modularization Statements 57

    4.1 Overview 57

    4.2 Procedures 584.2.1 Methods 584.2.2 Function Modules 594.2.3 Subroutines 60

    4.3 Dialog Modules 65

    4.4 Event Blocks 664.4.1 Program Constructor 664.4.2 Reporting Events 674.4.3 Selection Screen Events 724.4.4 List Events 78

    4.5 Source Text Modularization 844.5.1 Include Programs 844.5.2 Macros 85

    5 Built-In Types, Data Objects and Functions 89

    5.1 Overview 89

    5.2 Built-In Data Types 895.2.1 Built-In ABAP Types 895.2.2 Generic ABAP Types 935.2.3 Built-In Types in the ABAP Dictionary 945.2.4 Built-In Data Type Cursor 97

    5.3 Built-In Data Objects 975.3.1 The Constant space 975.3.2 The Self-Reference me 985.3.3 System Fields 985.3.4 The Screen Structure 103

    5.4 Built-In Functions 1035.4.1 Mathematical Functions 1035.4.2 Description Functions 105

    abapIVZ.fm Seite 8 Freitag, 31. Januar 2003 12:51 12

  • Contents 9

    Part 3 Declarative Statements 107

    6 Declarative Statements for Data Types and Data Objects 109

    6.1 Overview 1096.1.1 Validity and Visibility 1096.1.2 Data Types 1106.1.3 Data Objects 1106.1.4 Absolute Type Names 1126.1.5 Statements for Declaring Data Types and Data Objects 115

    6.2 Including Type Groups 115

    6.3 Defining Data Types 1166.3.1 Type Definitions with Built-In ABAP Types 1176.3.2 Type Definition Through Reference to Existing Types 1186.3.3 Defining Reference Types 1186.3.4 Defining Structured Types 1206.3.5 Defining Table Types 1216.3.6 Defining a Range Table Type 124

    6.4 Declaring Variables 1256.4.1 Additions for Data Objects 1266.4.2 Elementary Data Objects of Built-In ABAP Types 1286.4.3 Data Objects of Existing Types 1286.4.4 Declaring Reference Variables 1296.4.5 Declaring Structures 1306.4.6 Declaring Internal Tables 1316.4.7 Defining a Ranges Table 133

    6.5 Declaring Static Attributes of Classes 134

    6.6 Declaring Constant Data Objects 135

    6.7 Declaring Static Data Objects in Procedures 136

    6.8 Copying Structure Components 137

    6.9 Declaring the Table Work Areas 1396.9.1 Flat Table Work Area 1396.9.2 Any Table Work Area 140

    6.10 Declaring Field Symbols 1426.10.1 Typing Field Symbols 1436.10.2 Casting a Structure 143

    6.11 Declaring an Extract Dataset 144

    abapIVZ.fm Seite 9 Freitag, 31. Januar 2003 12:51 12

  • 10 Contents

    7 Defining Classes and Interfaces 147

    7.1 Overview 147

    7.2 Defining Classes 1477.2.1 Declaration Section 1487.2.2 Implementation Section 1557.2.3 Declaring Classes 156

    7.3 Defining Interfaces 1587.3.1 Declaration Section 1587.3.2 Declaring Interfaces 159

    7.4 Declaring Components in Classes and Interfaces 1607.4.1 Methods 1607.4.2 Events 1787.4.3 Implementing and Integrating Interfaces 181

    8 Typing 187

    8.1 Overview 187

    8.2 Syntax of Typing 1878.2.1 Generic Typing 1888.2.2 Complete Typing 189

    8.3 Checking the Typing 1908.3.1 General Rules 1908.3.2 Literals as Actual Parameters 190

    Part 4 Generating Objects 193

    9 Generating Objects and Data Objects 195

    9.1 Overview 195

    9.2 Generating Data Objects 1959.2.1 Defining the Data Type Implicitly 1969.2.2 Defining the Data Type Through Built-In ABAP Types 1979.2.3 Defining the Data Type Through an Existing Type 1999.2.4 Generating Reference Variables 2009.2.5 Generating Internal Tables 2019.2.6 Treatable Exceptions When Generating Data Objects 203

    9.3 Generating Objects in ABAP Objects 2039.3.1 Defining the Class Implicitly 2049.3.2 Defining the Class Explicitly 2059.3.3 Static Parameter Transfer 2069.3.4 Dynamic Parameter Passing 2079.3.5 Treatable Exceptions When Generating Objects 208

    abapIVZ.fm Seite 10 Freitag, 31. Januar 2003 12:51 12

  • Contents 11

    Part 5 Calling and Exiting Program Units 209

    10 Calling ABAP Programs 211

    10.1 Overview 211

    10.2 Calling Executable Programs 21210.2.1 Flow of the Program Call 21310.2.2 Additions for the Selection Screen 21510.2.3 Additions for the Basic List 22410.2.4 Additions for Background Processing 228

    10.3 Calling Transactions 23010.3.1 Calling a Transaction and Returning to the Calling Program 23010.3.2 Calling a Transaction Without Returning to the Calling Program 238

    11 Calling Processing Blocks 239

    11.1 Overview 239

    11.2 Calling Procedures 24011.2.1 Overview 24011.2.2 Method Calls 24311.2.3 Calling Function Modules 25511.2.4 Subroutine Call 264

    11.3 Calling Event Handlers 26911.3.1 Triggering Events 27011.3.2 Registering Event Handlers 271

    11.4 Calling Event Blocks 27411.4.1 Triggering Events of a Logical Database 27411.4.2 Triggering List Events 276

    12 Exiting Program Units 279

    12.1 Overview 279

    12.2 Exiting Programs 279

    12.3 Exiting Processing Blocks 28012.3.1 Procedure for Exiting Processing Blocks 28012.3.2 Exiting Processing Blocks with RETURN 28112.3.3 Exiting Processing Blocks with EXIT 28212.3.4 Exiting Processing Blocks with CHECK 28312.3.5 Exiting GET Processing Blocks with REJECT 28412.3.6 Exiting Processing Blocks of Executable Programs with STOP 285

    12.4 Exiting Loops 28612.4.1 Exiting Loops with EXIT 28612.4.2 Exiting a Loop Pass with CONTINUE 28712.4.3 Exiting a Loop Pass with CHECK 288

    abapIVZ.fm Seite 11 Freitag, 31. Januar 2003 12:51 12

  • 12 Contents

    Part 6 Program Flow Control 289

    13 Logical Expressions 291

    13.1 Overview 291

    13.2 Logical Expressions with Relational Operators 29113.2.1 Relational Operators for All Data Types 29113.2.2 Relational Operators for Character-Type Data Objects 29613.2.3 Relational Operators for Byte-Like Data Types 29813.2.4 Relational Operators for Bit Patterns 299

    13.3 Determining the Interval Range 300

    13.4 Checking States 30013.4.1 Checking the Assignment to a Field Symbol 30013.4.2 Checking the Validity of a Reference 30113.4.3 Checking a Data Object for the Initial Value 30213.4.4 Checking Output Parameters for Actual Parameters 30313.4.5 Checking Formal Parameters for Actual Parameters 303

    13.5 Evaluating a Selection Table 304

    13.6 Boolean Operators and Parenthetical Expressions 30713.6.1 Linking Logical Expressions with AND 30713.6.2 Linking Logical Expressions with OR 30713.6.3 Negating a Logical Expression with NOT 30813.6.4 Parenthetical Expressions 308

    14 Control Structures 311

    14.1 Overview 311

    14.2 Branching 31114.2.1 Branching with IF 31214.2.2 Branching with CASE 313

    14.3 Loops 31414.3.1 Unconditional Loops with DO 31414.3.2 Conditional Loops with WHILE 317

    15 Exception Handling 319

    15.1 Overview 31915.1.1 Treatable Exceptions Prior to Release 6.10 31915.1.2 Treatable Exceptions in Release 6.10 and Later 31915.1.3 Statements for Exceptions 320

    15.2 Class-Based Exceptions 32015.2.1 Exception Classes 32015.2.2 Triggering Class-Based Exceptions 322

    abapIVZ.fm Seite 12 Freitag, 31. Januar 2003 12:51 12

  • Contents 13

    15.2.3 System Response to a Class-Based Exception 32215.2.4 Handling Class-Based Exceptions 324

    15.3 Non-Class-Based Exceptions 32715.3.1 Defining Non-Class-Based Exceptions 32715.3.2 Triggering Non-Class-Based Exceptions 32715.3.3 Handling Non-Class-Based Exceptions 328

    15.4 Catchable Runtime Errors 32815.4.1 Defining Catchable Runtime Errors 32815.4.2 Triggering Catchable Runtime Errors 32915.4.3 Handling Catchable Runtime Errors 329

    Part 7 Assignments 333

    16 Value Assignments 335

    16.1 Overview 335

    16.2 Assigning Data Objects 33616.2.1 Assignment with Keyword 33616.2.2 Assignment with Assignment Operator 33616.2.3 Multiple Assignments 337

    16.3 Assigning Structure Components 338

    16.4 Formatted Assignment 339

    16.5 Converting Packed Numbers 341

    17 Setting References 343

    17.1 Overview 343

    17.2 Assigning Data Objects to Field Symbols 34317.2.1 Specifying a Memory Area 34417.2.2 Specifying the Data Type 35017.2.3 Specifying the Range Limits 353

    17.3 Initializing Field Symbols 356

    17.4 Retrieving a Data Reference 356

    18 Initializing Data Objects 359

    18.1 Overview 359

    18.2 Initializing Any Data Objects 35918.2.1 Initializing with the Initial Value 35918.2.2 Initializing with Other Contents 360

    18.3 Initializing Internal Tables 361

    18.4 Releasing Memory 362

    abapIVZ.fm Seite 13 Freitag, 31. Januar 2003 12:51 12

  • 14 Contents

    Part 8 Processing Internal Data 363

    19 Calculation Expressions 365

    19.1 Overview 365

    19.2 The COMPUTE Statement 365

    19.3 Arithmetic Expressions 36619.3.1 Arithmetic Operators 36719.3.2 Compounding 36719.3.3 Priority of Functional Methods 36819.3.4 Calculation Type 36819.3.5 Treatable Exceptions in Arithmetic Expressions 370

    19.4 Bit Expressions 37019.4.1 Bit Operators 37119.4.2 Compounding 372

    20 Calculation Statements 373

    20.1 Overview 373

    20.2 Addition 373

    20.3 Subtraction 374

    20.4 Multiplication 374

    20.5 Division 375

    21 Processing Byte/Character Strings 377

    21.1 Overview 37721.1.1 Byte Strings and Character Strings 37721.1.2 Statements for Processing Byte/Character Strings 37721.1.3 Operands in Byte/Character String Processing 37821.1.4 Treating Trailing Blanks During Character String Processing 379

    21.2 Concatenating Byte and Character Strings 380

    21.3 Searching Byte and Character Strings 38121.3.1 Searching with FIND 38121.3.2 Searching with SEARCH 384

    21.4 Replacing Byte and Character Strings 38821.4.1 Position-Based Replacement 38921.4.2 Pattern-Based Replacement 390

    21.5 Shifting Byte and Character Strings 39221.5.1 Number of Places 39321.5.2 Shifting Direction 39521.5.3 Shifting Certain Characters Out of the Field 395

    21.6 Splitting Byte and Character Strings 396

    abapIVZ.fm Seite 14 Freitag, 31. Januar 2003 12:51 12

  • Contents 15

    21.7 Condensing Character Strings 398

    21.8 Converting Character Strings 399

    21.9 Overlaying Character Strings 401

    21.10 Converting Character Strings 40221.10.1 Converting Upper/Lowercase 40221.10.2 Pattern-Based Conversion 403

    21.11 Setting and Reading Bits 40421.11.1 Setting Single Bits in Byte Strings 40421.11.2 Reading Single Bites from Byte Strings 405

    22 Processing Internal Tables 407

    22.1 Overview 407

    22.2 Reading Internal Tables 40822.2.1 Reading Single Lines 40822.2.2 Loop Processing for Internal Tables 41822.2.3 Control Level Processing 42222.2.4 Summation in Control Levels 425

    22.3 Filling Internal Tables 42622.3.1 Inserting Lines 42622.3.2 Inserting Condensed Lines 43122.3.3 Appending Lines 433

    22.4 Processing Table Lines 43722.4.1 Changing Lines 43722.4.2 Deleting Lines 44222.4.3 Sorting 448

    22.5 Specifying Components 451

    22.6 Reading Attributes of Internal Tables 452

    22.7 Searching Internal Tables 45422.7.1 Search Options 455

    22.8 Processing Special Internal Tables 456

    22.9 Editing Tables 461

    23 Processing Extract Datasets 463

    23.1 Introduction 463

    23.2 Defining the Row Structure 463

    23.3 Filling the Extract Dataset 465

    23.4 Sorting the Extract Dataset 466

    23.5 Reading the Extract Dataset 468

    23.6 Control Level Processing 469

    abapIVZ.fm Seite 15 Freitag, 31. Januar 2003 12:51 12

  • 16 Contents

    24 Attributes of Data Objects 473

    24.1 Overview 473

    24.2 Attributes of Any Data Objects 47324.2.1 Length of the Data Object 47424.2.2 Output Length 47424.2.3 Data Type and Number of Components 47524.2.4 Decimal Places 47724.2.5 Conversion Routines 47724.2.6 Help Text 477

    24.3 Attributes of Internal Tables 47924.3.1 Table Type 47924.3.2 Initial Memory Requirements 48024.3.3 Number of Rows 480

    24.4 Distance Between Data Objects 481

    abapIVZ.fm Seite 16 Freitag, 31. Januar 2003 12:51 12

  • Contents 491

    Contents

    Part 9 User Dialogs 503

    25 Dynpros 505

    25.1 Overview 505

    25.2 Flow Logic Statements 50625.2.1 Flow Logic Event Blocks 50825.2.2 Calling Dialog Modules 50925.2.3 Controlling Data Transfer 51125.2.4 Processing Chains 51425.2.5 Table Controls 51625.2.6 Calling a Subscreen 519

    25.3 ABAP Statements for Dynpros 52225.3.1 Calling Dynpro Sequences 52325.3.2 Setting the Next Dynpro 52425.3.3 Setting the GUI Status 52425.3.4 Determining the GUI Status 52625.3.5 Setting the GUI Title 52825.3.6 Determining Attributes of Dynpro Fields 52925.3.7 Modifying Attributes of Dynpro Fields 53125.3.8 Setting the Cursor 53225.3.9 Determining the Cursor Position 53525.3.10 Declaring a Control 53725.3.11 Initializing a Control 54325.3.12 Exiting the Step Loop 54325.3.13 Holding Field Contents 54425.3.14 Suppressing the Display 54525.3.15 Leaving a Dynpro 545

    26 Selection Screens 547

    26.1 Overview 54726.1.1 Selection Screens as Dynpros 54726.1.2 Selection Screen Tasks 54726.1.3 GUI Status of Selection Screens 54726.1.4 Selection Screen Events 54826.1.5 Selection Screens and Logical Databases 54826.1.6 Statements for Selection Screens 548

    26.2 Creating and Laying Out Selection Screens 54826.2.1 Creating Selection Screens 54926.2.2 Laying Out Selection Screens 55226.2.3 Adopting Elements of Other Selection Screens 56626.2.4 Variants and Additions for Selection Screens of Logical Databases 570

    abapIV2.fm Seite 491 Freitag, 31. Januar 2003 12:56 12

  • 492 Contents

    26.3 Defining Parameters 57426.3.1 Data Type of the Parameter 57626.3.2 Attributes of Screen Elements 57926.3.3 Attributes of the Value and the Passing of Values 58426.3.4 Additions for Selection Screens of Logical Databases 587

    26.4 Defining Selection Criteria 59026.4.1 Data Type of the LOW and HIGH Selection Table Columns 59326.4.2 Attributes of Screen Elements 59526.4.3 Attributes of the Value and Passing of Values 59826.4.4 Additions for Selection Screens of Logical Databases 60026.4.5 Enabling User-Defined Field Help 600

    26.5 Calling Selection Screens 60126.5.1 Calling Via SUBMIT 60226.5.2 Calling Via a Report Transaction 60226.5.3 Calling Via a Dialog Transaction 60226.5.4 Calling in the Program 60226.5.5 Selection Screen Processing 604

    27 Lists 605

    27.1 Overview 60527.1.1 Lists as Screens 60527.1.2 Lists in the ABAP Program 60527.1.3 Basic List 60527.1.4 Details Lists 60527.1.5 The Structure of a List 60627.1.6 Print Lists 60727.1.7 Lists and ABAP Objects 60727.1.8 Lists and Unicode 60727.1.9 Statements for List Processing 608

    27.2 Creating Lists 60927.2.1 Writing Data in Lists 60927.2.2 Creating Horizontal Lines 63427.2.3 Formatting Lists Section by Section 63527.2.4 Displaying Blank Lines 64227.2.5 Vertical Positioning of the List Cursor 64327.2.6 Horizontal Positioning of the List Cursor 64827.2.7 Fixed Areas When Scrolling Horizontally 64927.2.8 Page Breaks and Print Lists 65027.2.9 Conditional Page Breaks 65827.2.10 Saving Variables with List Lines 65927.2.11 Print List Page Margins 66127.2.12 Controlling Print Lists 661

    27.3 Processing Lists in the List Buffer 66527.3.1 Reading List Lines 66527.3.2 Modifying List Lines 668

    abapIV2.fm Seite 492 Freitag, 31. Januar 2003 12:56 12

  • Contents 493

    27.3.3 Scrolling Lists 67027.3.4 Reading List Attributes 673

    27.4 Evaluating the Displayed List at the Cursor Position 676

    27.5 Display Attributes of Screen Lists 67827.5.1 GUI Status of a Screen List 67827.5.2 Title of a Screen List 68027.5.3 Setting the Cursor 68027.5.4 Lists in a Dialog Box 683

    27.6 Calling and Exiting List Displays 68427.6.1 Calling the Basic List Display 68527.6.2 Exiting the List Display 686

    28 Messages 689

    28.1 Overview 68928.1.1 Storing Messages 68928.1.2 Message Types 689

    28.2 Sending Messages 69228.2.1 Variant 1 69228.2.2 Variant 2 69328.2.3 Variant 3 69328.2.4 Variant 4 69428.2.5 Addition Options 695

    Part 10 Processing External Data 699

    29 Open SQL 701

    29.1 Overview 70129.1.1 Scope of Open SQL 70129.1.2 Database Interface 70129.1.3 Database Access 70129.1.4 Client Handling 70129.1.5 SAP Buffering 70129.1.6 LUW 70229.1.7 Open SQL Statements 702

    29.2 Reading Data from Database Tables 70229.2.1 Determining the Structure of the Result Set 70429.2.2 Specifying Database Tables to Be Read 71129.2.3 Specifying the Target Area 71729.2.4 Restricting the Result Set 72229.2.5 Combining Lines 73429.2.6 Restricting Combined Lines 73529.2.7 Sorting Result set Lines 736

    abapIV2.fm Seite 493 Freitag, 31. Januar 2003 12:56 12

  • 494 Contents

    29.3 Reading Data from Database Tables Using a Cursor 73929.3.1 Open Cursor 73929.3.2 Reading Data Using a Cursor 74029.3.3 Close Cursor 742

    29.4 Inserting Data into Database Tables 74329.4.1 Specifying the Database Table 74429.4.2 Specifying the Source 745

    29.5 Changing Data in Database Tables 74729.5.1 Specifying the Database Table 74829.5.2 Specifying Changes 749

    29.6 Inserting or Changing Data in Database Tables 75429.6.1 Specifying the Database Table 75429.6.2 Specifying the Source 755

    29.7 Deleting Data in Database Tables 75629.7.1 Specifying the Database Table 75729.7.2 Specifying Lines 757

    29.8 Work Areas in Open SQL Statements 760

    29.9 Treatable Exceptions in Open SQL Statements 761

    30 Native SQL 763

    30.1 Overview 763

    30.2 Including Native SQL 76330.2.1 Host Variables 76530.2.2 Processing Cursors 76630.2.3 Calling Database Procedures 76830.2.4 Defining the Database Connection 76930.2.5 Implicit Cursor Processing 772

    30.3 Leaving Native SQL 773

    30.4 Treatable Exceptions in Native SQL 774

    31 Data Clusters 775

    31.1 Overview 775

    31.2 Creating Data Clusters 77531.2.1 Defining the Data Cluster 77631.2.2 Defining the Storage Medium 77731.2.3 Controlling Compression 78231.2.4 Treatable Exceptions During the Export of Data Clusters 782

    31.3 Reading Data Clusters 78331.3.1 Specifying the Data Objects to Be Read 78331.3.2 Defining the Storage Medium 78531.3.3 Conversion Additions 788

    abapIV2.fm Seite 494 Freitag, 31. Januar 2003 12:56 12

  • Contents 495

    31.3.4 Treatable Exceptions During the Import from Data Clusters 795

    31.4 Reading the Directory of a Data Cluster 795

    31.5 Deleting a Data Cluster 797

    31.6 Deleting a Data Cluster in the ABAP Memory 798

    32 The ABAP File Interface 799

    32.1 Overview 79932.1.1 Addressing Files 79932.1.2 Authorizations for Accessing Files 79932.1.3 Locks 80132.1.4 The File Interface in Unicode Programs 80132.1.5 File Size 80332.1.6 File Interface Statements 803

    32.2 Opening a File 80332.2.1 Defining the Access Type 80432.2.2 Defining the Storage Type 80532.2.3 Specifying the Position 81032.2.4 Operating System-Dependent Additions 81132.2.5 Error Handling 81332.2.6 Treatable Exceptions When Opening Files 815

    32.3 Writing a File 81532.3.1 The Influence of Access Type 81632.3.2 The Influence of Storage Type 81632.3.3 Restricting the Number of Transferred Characters or Bytes 81832.3.4 Treatable Exceptions When Writing to Files 818

    32.4 Reading a File 81932.4.1 The Influence of Access Type 82032.4.2 The Influence of Storage Type 82032.4.3 Restricting the Number of Imported Characters or Bytes 82132.4.4 Determining the Number of Imported Characters or Bytes 82332.4.5 Treatable Exceptions When Reading Files 823

    32.5 Determining the Attributes of an Opened File 82332.5.1 Determining the Position of the File Pointer 82432.5.2 Determining Other Attributes 82532.5.3 Treatable Exceptions When Determining File Attributes 827

    32.6 Changing the Attributes of an Opened File 82732.6.1 Defining the Position of the File Pointer 82832.6.2 Changing Other Attributes 82932.6.3 Treatable Exceptions When Changing File Attributes 831

    32.7 Closing a File 832

    32.8 Deleting a File 833

    abapIV2.fm Seite 495 Freitag, 31. Januar 2003 12:56 12

  • 496 Contents

    33 Data Consistency 835

    33.1 Overview 835

    33.2 Database LUW 83533.2.1 Database Commit 83633.2.2 Database Rollback 837

    33.3 SAP LUW 83733.3.1 SAP Commit 83833.3.2 SAP Rollback 84033.3.3 Local Updating 841

    33.4 Database Locks 842

    33.5 SAP Locks 84233.5.1 Imposing SAP Locks 84233.5.2 Lifting SAP Locks 843

    33.6 Authorization Check 844

    Part 11 Program Parameters 847

    34 Parameters in the SAP Memory 849

    34.1 Overview 849

    34.2 Setting Parameters 849

    34.3 Reading Parameters 850

    35 Language Environment 851

    35.1 Overview 851

    35.2 Setting the Language of Text Elements 851

    35.3 Setting the Text Environment 852

    35.4 Determining the Text Environment 854

    35.5 Setting Country Identification 855

    36 Time Stamp 857

    36.1 Overview 857

    36.2 Current Time Stamp 858

    36.3 Processing Time Stamps 85836.3.1 Expanding Time Stamps 85936.3.2 Creating Time Stamps 85936.3.3 Explanation of the Addition Option 859

    36.4 Current Time 861

    abapIV2.fm Seite 496 Freitag, 31. Januar 2003 12:56 12

  • Contents 497

    Part 12 Program Processing 863

    37 Testing and Checking Programs 865

    37.1 Overview 865

    37.2 Setting Breakpoints 865

    37.3 Measuring Runtime 86637.3.1 Relative Program Runtime 86637.3.2 Setting Time Resolution 86737.3.3 Measuring Section for Runtime Analysis 868

    37.4 Avoiding the Extended Program Check 869

    38 Generating Programs Dynamically 871

    38.1 Overview 871

    38.2 Dynamic Subroutine Pool 872

    38.3 Reading an ABAP Program 875

    38.4 Checking Syntax 876

    38.5 Creating an ABAP Program 878

    38.6 Reading a Text Pool 881

    38.7 Creating a Text Pool 882

    38.8 Calling the ABAP Editor 883

    Part 13 External Programming Interfaces 885

    39 Remote Function Call 887

    39.1 Overview 88739.1.1 Introduction 88739.1.2 Destination 88839.1.3 System Fields in RFC 88939.1.4 Exceptions in RFCs 88939.1.5 RFC Authorization 88939.1.6 Inadmissible Actions 89039.1.7 RFC Interface Statements 890

    39.2 Calling Remote Functions 89039.2.1 Synchronous Remote Function Call 89139.2.2 Asynchronous Remote Function Call 89239.2.3 Transactional Remote Function Call 899

    abapIV2.fm Seite 497 Freitag, 31. Januar 2003 12:56 12

  • 498 Contents

    40 XSLT Transformations 901

    40.1 Overview 901

    40.2 Call 90140.2.1 Specifying the Transformation 90240.2.2 Transformation Source 90240.2.3 Transformation Result 90340.2.4 Explanation of Addition Options 903

    Part 14 Obsolete Statements 907

    41 Obsolete Statements 909

    41.1 Overview 909

    41.2 Obsolete Syntax 909

    41.3 Obsolete Modularization 91041.3.1 Obsolete Event Block 910

    41.4 Obsolete Declarations 91041.4.1 Obsolete Interface Work Areas 91041.4.2 Obsolete Declarations of Internal Standard Tables 91341.4.3 Obsolete Declarations of Special Internal Tables 91541.4.4 Obsolete Note for the Extended Program Check 917

    41.5 Obsolete Object Creation 918

    41.6 Obsolete Program Call 920

    41.7 Obsolete Exiting of a Program 922

    41.8 Obsolete Flow Control 92341.8.1 Obsolete Relational Operators 92341.8.2 Obsolete Branching 924

    41.9 Obsolete Assignments 92541.9.1 Obsolete Assignment of a Percentage Subfield 92541.9.2 Obsolete Conversion 92641.9.3 Obsolete Temporary Storage of Data Objects 927

    41.10 Obsolete Calculation Statements 92841.10.1 Addition of Field Sequences in the Memory 92841.10.2 Adding Component-by-Component 93041.10.3 Subtracting Component-by-Component 93141.10.4 Multiplying Component by Component 93241.10.5 Dividing Component by Component 93341.10.6 Obsolete Calculations During List Creation 933

    41.11 Obsolete Character String Processing 93641.11.1 Obsolete Translation 93641.11.2 Obsolete Replacement 93841.11.3 Complement on Nine of a Date 940

    abapIV2.fm Seite 498 Freitag, 31. Januar 2003 12:56 12

  • Contents 499

    41.12 Obsolete Processing of Internal Tables 94141.12.1 Obsolete Key Specification When Reading Rows 94141.12.2 Obsolete Assignment of Table Lines 94441.12.3 Obsolete Form of the PROVIDE Statement 946

    41.13 Contexts 94941.13.1 Overview 94941.13.2 Creating Instances of Contexts 95041.13.3 Providing Contexts with Key Values 95041.13.4 Querying Contexts 951

    41.14 Obsolete Statements in the Flow Logic of Dynpros 95341.14.1 Checking Values in Flow Logic 95341.14.2 Processing Step Loops 955

    41.15 Obsolete Statements in List Processing 96041.15.1 Obsolete Formatting Statements 96041.15.2 Obsolete Print Parameters 961

    41.16 Obsolete Database Accesses 96341.16.1 Obsolete Reading of a Row 96341.16.2 Obsolete Sequential Reading of Several Rows 96641.16.3 Obsolete Reading of Several Rows in an Internal Table 96741.16.4 Obsolete Short Forms in Open SQL 968

    41.17 Obsolete External Programming Interfaces 97041.17.1 CPI-C Interface 97041.17.2 OLE Interface 977

    Part 15 Appendix 987

    A Conversion Rules for Assignments 989

    A.1 Overview 989

    A.2 Conversion Rules for Elementary Data Types 989A.2.1 Presenting Numeric Values in Character-Like Fields 990A.2.2 Conversion Table for Source Field Type c 991A.2.3 Conversion Table for Source Field Type d 992A.2.4 Conversion Table for Source Field Type f 993A.2.5 Conversion Table for Source Field of Types i, b, or s 994A.2.6 Conversion Table for Source Field Type n 996A.2.7 Conversion Table for Source Field Type p 997A.2.8 Conversion Table for Source Field Type string 998A.2.9 Conversion Table for Source Field Type t 999A.2.10 Conversion Table for Source Field Type x 1000A.2.11 Conversion Table for Source Field Type xstring 1001

    A.3 Conversion Rules for Structures 1001A.3.1 Conversion Between Flat Structures 1002A.3.2 Conversion Between Flat Structures and Single Fields 1006

    A.4 Conversion Rules for Internal Tables 1007

    abapIV2.fm Seite 499 Freitag, 31. Januar 2003 12:56 12

  • 500 Contents

    A.5 Assignments Between Reference Variables 1007A.5.1 Static and Dynamic Types 1008A.5.2 Narrowing Cast and Widening Cast 1008A.5.3 Assignments Between Data Reference Variables 1009A.5.4 Assignments Between Object Reference Variables 1010

    B Low-Level Classes and Interfaces 1013

    B.1 Auxiliary Classes 1013

    B.2 Run Time Type Identification Classes 1013

    B.3 Classes for Converting External Data Formats 1013

    B.4 Class for Extreme Values of Data Objects 1014

    B.5 Class for the Properties of Characters 1014

    B.6 Class for Mathematical Operations 1014

    B.7 Class for Time Stamp 1015

    B.8 Classes for Data Clusters 1015

    B.9 Class for Transactions 1016

    B.10 Class for Processing Lists 1016

    B.11 Object Services 1016

    B.12 JavaScript Integration 1017

    C Low-Level Function Modules 1019

    C.1 Function Modules for Print Parameters 1019C.1.1 GET_PRINT_PARAMETERS 1019C.1.2 SET_PRINT_PARAMETERS 1022

    C.2 Function Modules for Files on the Presentation Server 1022

    C.3 Function Modules for Calling Logical Databases 1022

    D Predefined Treatable Exceptions 1025

    D.1 Overview 1025

    D.2 Predefined Exception Classes 1025

    D.3 Catchable Runtime Errors 1028D.3.1 Exception Groups for Arithmetic Errors 1028D.3.2 Exception Group for Conversion Errors 1029D.3.3 Exception Group for Errors When Creating Data Objects 1030D.3.4 Exception Group for Errors When Creating Instances of Classes 1030D.3.5 Exception Group for Errors When Accessing Data Objects 1030D.3.6 Exception Group for Errors in Dynamic Method Calls 1031D.3.7 Exception Group for Errors When Accessing Files 1032D.3.8 Exception Group for Errors When Accessing Data Clusters 1032

    abapIV2.fm Seite 500 Freitag, 31. Januar 2003 12:56 12

  • Contents 501

    D.3.9 Exception Group for Errors in the Language Environment 1033D.3.10 Exception Group for Errors with Remote Function Calls 1033D.3.11 Catchable Runtime Errors That Are Not Assigned to Any Group 1033

    E Notes on the CD ROMs 1035

    F Glossary 1037

    Index 1079

    abapIV2.fm Seite 501 Freitag, 31. Januar 2003 12:56 12

  • Defining Classes and Interfaces 147

    7 Defining Classes and Interfaces

    7.1 Overview

    This chapter describes the definition of classes and interfaces, as well astheir components. Classes and interfaces are the foundation of ABAPObjects, the object-oriented component of the ABAP language. You candefine classes and interfaces in ABAP programs with the following pro-gram types (see Table 3.1):

    In a class pool, you can use the Class Builder in the ABAP Workbench todefine one global class of the class library. This global class can be usedin all other ABAP programs. You can also define local classes and inter-faces 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 globalinterface can be used in all other ABAP programs. You cannot definelocal classes or interfaces in an interface pool.

    In all other ABAP programs (except type groups), you can define localclasses and interfaces for use in the program itself.

    In addition to the declarations described in Chapter 6, you can declareadditional components within classes and interfaces. These componentsare also described in this chapter.

    The statements for declaring classes and interfaces are:

    7.2 Defining Classes

    The full definition of a class consists of a declaration section and an imple-mentation section, both of which you introduce with CLASS. You define

    Statement Section

    CLASS 7.2

    INTERFACE 7.3

    METHODS, CLASS-METHODS 7.4.1

    EVENTS, CLASS-EVENTS 7.4.2

    INTERFACES, ALIASES 7.4.3

    CLASS

    KAP07.fm Seite 147 Freitag, 31. Januar 2003 2:28 14

  • 148 Defining Classes and Interfaces

    the properties of a class and declare its components in the declarationsection. You implement the methods of the class in the implementationsection. You use other variants of CLASS to declare classes in a program.

    7.2.1 Declaration Section

    Defines a class and declares its components.

    Syntax

    The statement block CLASS class DEFINITION ENDCLASS defines aclass. You declare components of the class, components, betweenCLASS and ENDCLASS. You have to specify each component after one ofthe statements PUBLIC SECTION, PROTECTED SECTION, or PRIVATESECTION, and you have to list those in the specified order. A class doesnot have to contain all the SECTION statements.

    You can use the additions of the CLASS statement to publish a class glo-bally in the class library, define an inheritance relationship, make the classabstract or final, define whether it can be instantiated, and offer friendshipto other classes and/or interfaces.

    7.2.1.1 Visibility Areas

    Public visibility area

    Syntax

    This statement defines the public visibility area of class class. All classcomponents that you declare in the area after the PUBLIC SECTIONstatement can be addressed from outside the class, from within its sub-classes, and within the class itself.

    CLASS class DEFINITION [PUBLIC] [INHERITING FROM superclass] [ABSTRACT] [FINAL] [CREATE {PUBLIC|PROTECTED|PRIVATE}] [[GLOBAL] FRIENDS [classi] [ifaci]]. [PUBLIC SECTION]. [components] [PROTECTED SECTION]. [components] [PRIVATE SECTION]. [components]ENDCLASS.

    PUBLIC SECTION.

    KAP07.fm Seite 148 Freitag, 31. Januar 2003 2:28 14

  • Defining Classes 149

    Protected visibility area

    Syntax

    This statement defines the protected visibility area of class class. Allclass components that you declare in the area after the PROTECTED SEC-TION statement can be addressed in the subclasses of the class and withinthe class itself.

    Private visibility area

    Syntax

    This statement defines the private visibility area of class class. All classcomponents that you declare in the area after the PRIVATE SECTIONstatement can be addressed only within the class itself.

    7.2.1.2 Class Components

    Syntax

    You define the components of the classes in the visibility areas. The fol-lowing declaration statements are possible for components:

    TYPE-POOLS, TYPES, DATA, CLASS-DATA, and CONSTANTS for datatypes and data objects (see Chapter 6)

    METHODS, CLASS-METHODS, EVENTS, and CLASS-EVENTS for methodsand events (see Section 7.4)

    INTERFACES for implementing interfaces and ALIASES for alias namesof interface components (see Section 7.4)

    NoteAll the components of a class lie within one namespace. The name of acomponent must be unique within a class, regardless of its type (datatype, attribute, method, event, or alias name). The components of animplemented interface differ from the directly declared components ofthe class through prefix ifac~ (name of the interface with the interfacecomponent selector).

    PROTECTED SECTION.

    PRIVATE SECTION.

    components.

    KAP07.fm Seite 149 Freitag, 31. Januar 2003 2:28 14

  • 150 Defining Classes and Interfaces

    7.2.1.3 Global Classes

    Syntax

    When you use the addition PUBLIC, the class becomes a global class ofthe class library. The addition PUBLIC is possible for only one class of aclass pool and is generated by the Class Builder when you create a globalclass. All classes without the addition PUBLIC are local classes of theirprogram.

    NoteYou cannot use the TYPES statement to declare separate data types in thepublic visibility area of a global class. Moreover, you can use only globallydeclared data types there.

    7.2.1.4 Defining Subclasses

    Syntax

    If you use the addition INHERITING FROM, the class is derived from thesuperclass superclass through inheritance, and therefore becomes itsdirect subclass. The superclass superclass can be any non-final class thatis visible at this point.

    Each class can have only one direct superclass, but can have several directsubclasses (simple inheritance). Any class without the addition INHERIT-ING FROM implicitly inherits from the predefined empty class object.Taken together, all the classes in ABAP Objects form an inheritance tree, inwhich a clearly defined path exists from each class to the root node,object.

    The class class inherits the components of its superclass superclasswithout changing their visibility area. Only the components of the publicand protected visibility areas of the superclass are visible in the subclass.The attributes of the inherited components cannot be changed. You canalso declare additional components in a subclass and redefine inheritedmethodsthat is, you can re-implement them without changing theinterface.

    ... PUBLIC ...

    ... INHERITING FROM superclass ...

    KAP07.fm Seite 150 Freitag, 31. Januar 2003 2:28 14

  • Defining Classes 151

    NoteThe public and protected components of all the classes within a path ofthe inheritance tree all lie within the same namespace. New componentsin a subclass may not have names that are identical to public or protectedcomponents that were inherited from the superclasses.

    7.2.1.5 Abstract Classes

    Syntax

    The addition ABSTRACT defines an abstract class, class. You cannot gen-erate instances of an abstract class. To use the instance components of anabstract class, you have to instantiate a non-abstract subclass of the class.

    7.2.1.6 Final Classes

    Syntax

    The addition FINAL defines a final class class. You cannot derive sub-classes of a final class. All methods of a final class are implicitly final, whichmeans that you cannot explicitly declare them as final.

    NoteIf a class is both abstract and final, you can use only its static components.While you can declare instance components, you cannot use them.

    ExampleIn 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 classobject. You can access m1 in c2, but a1.

    CLASS c1 DEFINITION ABSTRACT. PROTECTED SECTION. METHODS m1. PRIVATE SECTION. DATA a1 TYPE string VALUE `Attribute A1 of class C1`.ENDCLASS.

    CLASS c2 DEFINITION INHERITING FROM c1 FINAL. PUBLIC SECTION.

    ... ABSTRACT ...

    ... FINAL ...

    KAP07.fm Seite 151 Freitag, 31. Januar 2003 2:28 14

  • 152 Defining Classes and Interfaces

    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

    The addition CREATE defines the context in which class can be instanti-atedthat is, where the CREATE OBJECT statement can be executed forthat class.

    A class with the addition CREATE PUBLIC can be instantiated wher-ever that class is visible.

    A class with the addition CREATE PROTECTED can be instantiated onlywithin methods of its subclasses and within the class itself.

    A class with the addition CREATE PRIVATE can be instantiated onlywithin methods the class itself. This means that you cannot instantiatethem as inherited components of subclasses, either.

    Whether or not a class can be instantiated also depends on its directsuperclass:

    Direct subclasses of object or a class with addition CREATE PUBLICimplicitly inherit the addition CREATE PUBLIC. You can explicitly spec-ify any CREATE addition to override the inherited one.

    ... CREATE {PUBLIC|PROTECTED|PRIVATE} ...

    KAP07.fm Seite 152 Freitag, 31. Januar 2003 2:28 14

  • Defining Classes 153

    Direct subclasses a class with addition CREATE PROTECTED implicitlyinherit the addition CREATE PROTECTED. You can explicitly specify anyCREATE addition to override the inherited one.

    Direct subclasses of a class with addition CREATE PRIVATE that are notfriends of that class are explicitly assigned the addition CREATE NONE.You cannot instantiate these classes, nor can you specify any explicitCREATE additions. Direct subclasses that are friends of the class implic-itly inherit the addition CREATE PRIVATE. You can explicitly specify allCREATE additions for friends of superclasses that can be instantiated asprivate to override the inherited one.

    NoteIf a class can be instantiated as private, we recommend making it final aswell, as its subclasses cannot be instantiated if they are not friends of theclass.

    7.2.1.8 Friends

    Syntax

    The addition FRIENDS makes class class a friend of classes classi and/or interfaces ifaci. At the same time, all subclasses of classes classi, allclasses that implement one of the interfaces ifaci, and all interfaces thathave one of the interfaces ifaci as a component interface become friendsof class. You have to specify at least one class or interface.

    The friends of a class have unrestricted access to the protected and privatecomponents of that class, and can generate unrestricted instances of theclass.

    The friends of class are not automatically friends of subclasses of class.Moreover, the addition FRIENDS does not make class a friend of itsfriends.

    If you do not use the GLOBAL additions, you can specify all classes andinterfaces for classi and ifaci that are visible at this point. If you defineglobal classes and interfaces in the class library as friends, please note thatin 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 PUBLICfor the global class of a class poolat the same time. You can list other

    ... [GLOBAL] FRIENDS [classi] [ifaci].

    KAP07.fm Seite 153 Freitag, 31. Januar 2003 2:28 14

  • 154 Defining Classes and Interfaces

    global classes and interfaces from the class library after GLOBAL FRIENDS.This addition is generated when the Class Builder creates a global classand you have specified friends at the corresponding tab page of the ClassBuilder.

    ExampleIn the example below, class c2 is a friend of interface i1, and thereforealso of the implementing class c1. It can instantiate these objects andaccess 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

    A separate statement exists for this situation.

    Syntax

    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 atleast one class or interface.

    CLASS class DEFINITION LOCAL FRIENDS [classi] [ifaci].

    KAP07.fm Seite 154 Freitag, 31. Januar 2003 2:28 14

  • Defining Classes 155

    This section does not introduce a declaration section, and you cannotcomplete it with ENDCLASS. You have to specify it in the class pool beforethe definition of the local classes and interfaces, classi and ifaci, if youwant them to have access to the private and protected components of theglobal class, class.

    7.2.2 Implementation Section

    Implements the methods of a class.

    Syntax

    In the statement block CLASS class IMPLEMENTATION ENDCLASS, youhave to implement the following methods of a class (in any sequence):

    All non-abstract methods that are declared in the declaration section ofthe class with METHODS or CLASS-METHODS

    All non-abstract methods of interfaces that are declared in the declara-tion section of the class with the INTERFACES statement

    All the methods inherited from superclasses that are listed in the decla-ration segment of the class with the statement METHODS ... REDEF-INITION

    The implementation of each method corresponds to a METHOD END-METHOD processing block (see Section 4.2.1). No statements are allowedoutside of method implementations in the implementation section.Within a method implementation, you can access all the components ininstance methods and all static components of their own class in staticmethods. The component selector is not necessary to address compo-nents in their own class. An implicitly generated local reference variablecalled me is available in the implementation of every instance method. mepoints to the current instance of the method.

    NoteA class thatin accordance with its declaration sectiondoes not haveto implement any methods has either a blank implementation section ornone at all.

    CLASS class IMPLEMENTATION. ... METHOD ... ... ENDMETHOD. ...ENDCLASS.

    KAP07.fm Seite 155 Freitag, 31. Januar 2003 2:28 14

  • 156 Defining Classes and Interfaces

    ExampleIn the following example, three methods of class c2 have to be imple-mented. Method m1 in c1 is abstract and does not have to be imple-mented.

    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

    In some cases, you have to explicitly declare the definition of a classbefore you can use it.

    Syntax

    CLASS class DEFINITION { DEFERRED | LOAD }.

    KAP07.fm Seite 156 Freitag, 31. Januar 2003 2:28 14

  • Defining Classes 157

    The two variants of the CLASS statement serve to declare a class class inthe program independently of the location of the actual definition. Theydo not introduce a declaration section, and you cannot complete themwith ENDCLASS. The additions have the following meanings:

    The variant with addition DEFERRED declares a local class before itsactual definition in the program. The program has to have a declarationsection for class later in the program. This statement is required ifyou want to refer to a class before it is actually defined. Access to indi-vidual components is not possible before the actual definition.

    The variant with addition LOAD loads a global class from the classlibrary. This statement is required prior to Release 6.40 if you want toaccess one of the static components of class within a program, or ifyou want to declare an event handler for class before class is loadedautomatically. All other accesses load class automatically. In Release6.40 and later, the addition LOAD is required only when the compilingof an ABAP program fails because it contains recursive accesses to aglobal class. In such cases, you may be able to make the program com-pilable by explicitly loading the class before the recursion.

    ExampleIn the following example, class c1 uses class c2 and vice-versa. As aresult, one of the classes has to be declared before its actual definition. Inaddition, global class cl_gui_cfw is loaded explicitly before one of itsstatic attributes is used.

    CLASS c1 DEFINITION DEFERRED.

    CLASS c2 DEFINITION. PUBLIC SECTION. DATA c1ref TYPE REF TO c1.ENDCLASS.

    CLASS c1 DEFINITION. PUBLIC SECTION. DATA c2ref TYPE REF TO c2.ENDCLASS.

    CLASS cl_gui_cfw DEFINITION LOAD.

    DATA state LIKE cl_gui_cfw=>system_state.

    KAP07.fm Seite 157 Freitag, 31. Januar 2003 2:28 14

  • 158 Defining Classes and Interfaces

    7.3 Defining Interfaces

    The definition of an interface consists of a declaration section that isintroduced with INTERFACE. In contrast to classes, interfaces do not havean implementation section. You use other variants of INTERFACE todeclare interfaces in a program.

    7.3.1 Declaration Section

    Defines an interface and declares its components.

    Syntax

    The statement block INTERFACE ENDINTERFACE defines an interfaceifac. You declare the components of the interface between INTERFACEand ENDINTERFACE.

    7.3.1.1 Interface Components

    Syntax

    The following declaration statements are possible for components:

    TYPE-POOLS, TYPES, DATA, CLASS-DATA, and CONSTANTS for datatypes and data objects (see Chapter 6)

    METHODS, CLASS-METHODS, EVENTS, and CLASS-EVENTS for methodsand events (see Section 7.4)

    INTERFACES for integrating component interfaces and ALIASES foralias names of their components (see Section 7.4)

    NoteAll the components of an interface lie within one namespace. The nameof a component must be unique within an interface, regardless of its type(data type, attribute, method, event, or alias name). The components ofan integrated interface differ from the directly declared componentsthrough prefix ifac~ (name of the interface with the interface compo-nent selector).

    INTERFACE

    INTERFACE ifac. [components]ENDINTERFACE.

    components

    KAP07.fm Seite 158 Freitag, 31. Januar 2003 2:28 14

  • Defining Interfaces 159

    ExampleIn the following example, an interface, i1, is declared with three interfacecomponents, a1, m1, and e1. Class c1 implements the interface, whichmakes the interface components public components of the class that canbe 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

    In some cases, you have to explicitly declare the definition of an interfacebefore you can use it.

    Syntax

    The two variants of the INTERFACE statement serve to declare interfaceifac independently of the location of the actual definition in the pro-gram. They do not introduce a declaration section, and you cannot com-plete them with ENDINTERFACE. The meanings of the two statements forinterfaces are identical to the meanings of the CLASS statementsdescribed for classes in Section 7.2.3.

    INTERFACE ifac { DEFERRED | LOAD }.

    KAP07.fm Seite 159 Freitag, 31. Januar 2003 2:28 14

  • 160 Defining Classes and Interfaces

    7.4 Declaring Components in Classes and Interfaces

    You use the declaration section of classes and interfaces to declare thecomponents of classes and interface that are major components of aclass:

    Attributes

    Methods

    Events

    Attributes are the data objects of a class. You can also declare class-spe-cific data types. You declare attributes and data types with the generalstatements TYPE-POOLS, TYPES, DATA, CLASS-DATA, and CONSTANTS,whichwith the exception of CLASS-DATAcan also be used in othercontexts and are described in Chapter 6.

    To declare methods and events, you use specific statements that are pos-sible only in the declaration section of classes and interfaces.

    7.4.1 Methods

    You use the METHODS and CLASS-METHODS statements to declare meth-ods. They define how the class responds. You define the interface of amethod in the declaration. Different categories of methods exist for spe-cific tasks:

    General methods

    Functional methods

    Constructors

    Event handlers

    Any of these methods can be declared as an instance method or a staticmethod.

    7.4.1.1 Instance Methods

    The METHODS statement declares instance methods. Instance methods aretied to objects. To use instance methods, you first have to generate anobject of the class. In instance methods, you can access all components ofthe methods own class without requiring a component selector.

    METHODS

    KAP07.fm Seite 160 Freitag, 31. Januar 2003 2:28 14

  • Declaring Components in Classes and Interfaces 161

    7.4.1.2 General Instance Methods

    The most general form of the METHODS statement lets you define instancemethods with any input and output parameters.

    Syntax

    This statement declares a general instance method, meth. The additionsABSTRACT and FINAL make the method abstract and final, respectively.

    The other additions define the parameter interface of the method anddefine which exceptions the method can propagate and/or trigger.

    Parameter interface

    The additions IMPORTING, EXPORTING, and CHANGING define the signa-ture of method meth:

    IMPORTING defines input parametersWhen you call the method, you have to specify a suitable actualparameter for each non-optional input parameter. The content of theactual parameter is passed on to the input parameter during the call.

    EXPORTING defines output parametersWhen you call the method, you can specify a suitable actual parameterfor each output parameter. The content of the output parameter ispassed on to the actual parameter when the method ends free oferrors.

    CHANGING defines input/output parametersWhen you call the method, you have to specify a suitable actualparameter for each non-optional input/output parameter. The contentof the actual parameter is passed on to the input/output parameterduring the call, and the content of the input/parameter is passed on tothe actual parameter when the method ends.

    You define the corresponding formal parameters by specify a list, para-meteri, after each addition.

    METHODS meth [ABSTRACT|FINAL] [IMPORTING {parameteri} [PREFERRED PARAMETER p]] [EXPORTING {parameteri}] [CHANGING {parameteri}] [{RAISING|EXCEPTIONS} {exci}].

    KAP07.fm Seite 161 Freitag, 31. Januar 2003 2:28 14

  • 162 Defining Classes and Interfaces

    Syntax of parameteri

    VALUE or REFERENCE defines whether a parameter, pi, is passed on as avalue or a reference. The parameter is passed on as a reference by defaultonly when you specify a name pi. An input parameter that is passed on asa reference cannot be changed in the method.

    You have to use the addition typing to type each formal parameter. Thesyntax of typing is described in Section 8.2. When you type a formalparameter, that typing is checked against the data type of any actualparameter that is passed on (see Section 8.3). The defined type alsodetermines the operand positions where the formal parameter(s) can beused in the method.

    You can use OPTIONAL or DEFAULT to define input parameters and input/output parameters as optional parameters; if you use DEFAULT, you canalso specify a default parameter defi. If a parameter is optional, you donot have to specify an actual parameter when you call the method. If youuse the addition OPTIONAL, the formal parameter is initialized type-spe-cifically; if you use DEFAULT, it assumes the value of the default parame-ter, defi. defi can be any data object with a suitable type that is visibleat this point.

    You can use PREFERRED PARAMETER to flag an input parameter p in listparameteri (after IMPORTING) as the preferred parameter. This additionmakes sense, however, only if all the input parameters are optional. If youcall the method with syntax

    [CALL METHOD] meth( a ).

    then the actual parameter a is assigned to preferred parameter p.

    NoteIf a formal parameter is typed as a reference variable and cannot bechanged in the procedure, the type check is performed as it is for a nar-rowing cast. If it can be changed in the procedure, the actual parametermust be compatible with the formal parameter.

    ExampleMethod read_spfli_into_table in the following example has oneinput parameter and one output parameter, both of which are completelytyped through references to the ABAP Dictionary.

    ... { {VALUE(pi)}|{REFERENCE(pi)}|{pi} {typing} [OPTIONAL|{DEFAULT defi}] } ...

    KAP07.fm Seite 162 Freitag, 31. Januar 2003 2:28 14

  • Declaring Components in Classes and Interfaces 163

    CLASS flights DEFINITION. PUBLIC SECTION. METHODS read_spfli_into_table IMPORTING VALUE(id) TYPE spfli-carrid EXPORTING flight_tab TYPE spfli_tab. ...ENDCLASS.

    Class-based exceptions

    Syntax

    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 orpropagated into the method of the ABAP runtime environment or withthe 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 abovesuperclasses and are visible at this point. You have to enter the exceptionclasses in the ascending order of their inheritance hierarchy.

    If an exception of a superclass occurs in the method and is neither han-dled nor passed on, this will cause either a syntax error or an exceptionthat the calling program has to handle, CX_SY_NO_HANDLER

    Notes

    Exceptions that involve the subclasses of CX_STATIC_CHECK and CX_DYNAMIC_CHECK have to either be handled within the method orpassed on explicitly with the addition RAISING. This is checked duringthe syntax check for CX_STATIC_CHECK, and at runtime for CX_DYNAMIC_CHECK.

    You cannot use the CATCH SYSTEM-EXCEPTIONS statement in amethod that propagates class-based exceptions with addition RAIS-ING. You have to trap these exceptions in a TRY block instead (seeChapter 15).

    ExampleIn class math below, method divide_1_by propagates all the exceptionsrepresented by class CX_SY_ARITHMETIC_ERROR and its subclasses. If aninput parameter, operand, is given a 0 value during a call, for example,

    ... RAISING {exci} ...

    KAP07.fm Seite 163 Freitag, 31. Januar 2003 2:28 14

  • 164 Defining Classes and Interfaces

    exception CX_SY_ZERODIVIDE is triggered, propagated, andas illus-trated in the examplehandled by the calling program in a TRY block.

    CLASS math DEFINITION. PUBLIC SECTION. METHODS divide_1_by IMPORTING operand TYPE I EXPORTING result TYPE f RAISING cx_sy_arithmetic_error.ENDCLASS.

    CLASS math IMPLEMENTATION. METHOD divide_1_by. result = 1 / operand. ENDMETHOD.ENDCLASS.

    ...

    DATA oref TYPE REF TO math.DATA exc TYPE REF TO cx_sy_arithmetic_error.

    DATA result TYPE f.DATA text TYPE string.

    CREATE OBJECT oref.TRY. oref->divide_1_by( EXPORTING operand = ... IMPORTING result = result ). text = result. CATCH cx_sy_arithmetic_error INTO exc. text = exc->get_text( ).ENDTRY.MESSAGE text TYPE 'I'.

    Exceptions prior to Release 6.10

    Syntax

    You use the addition EXCEPTIONS to define a list of exceptions, exci,that can be triggered in the method with the RAISE or MESSAGE RAIS-ING statements. You can specify any identifier exci for the definedexceptions; you must specify them directly. Exceptions defined in this

    ... EXCEPTIONS {exci} ...

    KAP07.fm Seite 164 Freitag, 31. Januar 2003 2:28 14

  • Declaring Components in Classes and Interfaces 165

    manner are tied to the methodssimilar to formal parametersand can-not be propagated.

    If an exception of this type is triggered in a method and not handled withthe addition EXCEPTIONS in the CALL METHOD statement during themethod call, a runtime error occurs.

    NoteYou cannot use the additions RAISING and EXCEPTIONS at the sametime. We recommend using class-based exceptions for new develop-ments in Release 6.10 and later, as they are independent of any specificmethod.

    ExampleIn class math below, a separate exception arith_error is defined formethod divide_1_by. This exception is triggered with the RAISE state-ment when an arithmetic error occurs. For example, if an input parame-ter, operand, is given a 0 value during a call, exception arith_error istriggered during the internal-method handling of exception CX_SY_ZERODIVIDE and is handled during the method call.

    CLASS math DEFINITION. PUBLIC SECTION. METHODS divide_1_by IMPORTING operand TYPE I EXPORTING result TYPE f EXCEPTIONS arith_error.ENDCLASS.

    CLASS math IMPLEMENTATION. METHOD divide_1_by. TRY. result = 1 / operand. CATCH cx_sy_arithmetic_error. RAISE arith_error. ENDTRY. ENDMETHOD.ENDCLASS.

    ...

    DATA result TYPE f.DATA text TYPE string.

    DATA oref TYPE REF TO math.

    KAP07.fm Seite 165 Freitag, 31. Januar 2003 2:28 14

  • 166 Defining Classes and Interfaces

    DATA exc TYPE REF TO cx_sy_arithmetic_error.

    CREATE OBJECT oref.oref->divide_1_by( EXPORTING operand = ... IMPORTING result = result EXCEPTIONS arith_error = 4 ).

    IF sy-subrc = 0. WRITE result.ELSE. WRITE 'Arithmetic error!'.ENDIF.

    Abstract methods

    Syntax

    The addition ABSTRACT is possible only in classes, not in interfaces. Theaddition ABSTRACT defines an abstract method, meth. An abstractmethod is not implemented in the implementation section of its class,and can be declared only in abstract classes. To implement an abstractmethod, you have to redefine it in a non-abstract subclass with the addi-tion REDEFINITION.

    NoteAlthough abstract methods can be defined in classes that are bothabstract and final, they cannot be implemented, which means you cannotuse them.

    Final methods

    Syntax

    The addition FINAL is possible only in classes, not in interfaces. The addi-tion FINAL defines a final method, meth. A final method cannot be rede-fined in a subclass. All methods in final classes are automatically final, andthe addition FINAL is not allowed here.

    7.4.1.3 Functional Instance Methods

    Functional methods can have any number of input parameters, and haveexactly one return value as output parameter.

    ... ABSTRACT ...

    METHODS ... FINAL ...

    KAP07.fm Seite 166 Freitag, 31. Januar 2003 2:28 14

  • Declaring Components in Classes and Interfaces 167

    Syntax

    This statement declares a functional instance method, meth. The addi-tions ABSTRACT, FINAL, IMPORTING, RAISING, and EXCEPTIONS havethe same functions as for general instance methods.

    Instead of the additions EXPORTING and CHANGING, a functional methodhas 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 samerules apply as for the typing of IMPORTING, EXPORTING, and CHANGINGparameters (see Section 8.2), with the exception that a return valuealways has to be completely typed: You cannot specify any of the generictypes from Table 5.3 for typing.

    You can use functional methods in the operand positions of statementswhere a data object is expected, as described in Section 2.2.5. The com-plete typing of the return value defines the data type of the operand.When you execute statements of this type, the functional method iscalled, and the content of the return value replaces the content of theoperand.

    Notes

    When you use a functional method in an operand position, class-basedexceptions that the method propagates with RAISING can be handledin a TRY block or propagated further, as with the general methods. Incontrast, the exceptions for a functional method that you define withEXCEPTIONS cannot be handled in operand positions, and will alwayscause a runtime error.

    If a functional method has the same name as a built-in function (seeSection 5.4), the expression meth( a ) in an operand position alwayscalls the functional method. This case can occur only in methods of theclass in the functional method, however.

    ExampleFunctional method factorial in the following example has a returnvalue, fact, with type i. The COMPUTE statement illustrates the methodcall in an operand position.

    METHODS meth [ABSTRACT|FINAL] [IMPORTING {parameteri}] RETURNING VALUE(r) {TYPE type_spec}|{LIKE dobj_spec} [{RAISING|EXCEPTIONS} {exci}].

    KAP07.fm Seite 167 Freitag, 31. Januar 2003 2:28 14

  • 168 Defining Classes and Interfaces

    CLASS math DEFINITION. PUBLIC SECTION. METHODS factorial IMPORTING n TYPE i RETURNING value(fact) TYPE i.ENDCLASS.

    CLASS math IMPLEMENTATION. METHOD factorial. fact = 1. IF n = 0. RETURN. ELSE. DO n TIMES. fact = fact * sy-index. ENDDO. ENDIF. ENDMETHOD.ENDCLASS.

    ...

    DATA oref TYPE REF TO math.DATA result TYPE i.

    CREATE OBJECT oref.COMPUTE result = oref->factorial( ... ).

    7.4.1.4 Instance Constructors

    Instance constructors are methods with the predefined name construc-tor that are called automatically during the instantiation of their class.Constructors can have any number of input parameters, but no outputparameters.

    Syntax

    This statement is possible only in the public visibility area of the declara-tion section of a class. It declares the instance constructor of the class,constructor.

    METHODS constructor [FINAL] [IMPORTING {parameteri}] [{RAISING|EXCEPTIONS} {exci}].

    KAP07.fm Seite 168 Freitag, 31. Januar 2003 2:28 14

  • Declaring Components in Classes and Interfaces 169

    Each class has a predefined method called constructor in its public vis-ibility area. You can use the explicit declaration to define the interface ofmethod constructor class-specifically and to implement its functional-ity. If you do not declare it explicitly, the instance constructor assumes thesignature from the instance constructor in the direct superclass and callsit implicitly.

    If you implement the instance constructor in a subclass, you have to callthe instance constructor of the superclass explicitly (with the pseudo ref-erence super->constructor) and supply its interface with data. Theonly exceptions to this are the direct subclasses of the root node, object.Prior to the call of the superclass constructor, an instance constructor canaccess only the static components of its class. After the superclass con-structor is called, it can also access the instance constructors.

    You can use the addition IMPORTING to define input parameters, accord-ing to the same rules as for the general methods. The additions RAISINGand EXCEPTIONS for propagating and defining exceptions also have thesame meaning as under the general methods.

    The instance constructor is called once for each instance of a class, withthe CREATE OBJECT statement, directly after the instance is generated. Inthe process, actual parameters must be assigned for all non-optional inputparameters, and exceptions can be handled or passed on. A call usingCALL METHOD is not possible, except in the call of the superclass con-structor with super->constructor in the redefined constructor of asubclass.

    Instance constructors are implicitly final. You can specify the additionFINAL, but it is not necessary.

    Notes

    Instance constructors represent an exception to the rule that all publiccomponents along a path in the inheritance tree have to line in thesame namespace. The instance constructor of each class has a separateinterface and a separate implementation. You cannot redefine aninstance constructor.

    Instance constructors are declared only in the public visibility area of aclass for technical reasons. You control the actual visibility with theaddition CREATE {PUBLIC|PROTECTED|PRIVATE} of the CLASSDEFINITION statement.

    KAP07.fm Seite 169 Freitag, 31. Januar 2003 2:28 14

  • 170 Defining Classes and Interfaces

    ExampleIn the following example, class c2 inherits from class c1. The instanceconstructor, constructor, is declared explicitly in both classes. It there-fore has to be implemented in both classes. In addition, the implementa-tion 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 inter-face. The only possible formal parameters of an event handler are theinput 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]].

    KAP07.fm Seite 170 Freitag, 31. Januar 2003 2:28 14

  • Declaring Components in Classes and Interfaces 171

    This statement declares the instance method meth as the event handlerevt of class class or interface ifac class. ifac can be any classes orinterfaces that are visible at this point and that contain a visible event evtas a component.

    If event evt is an instance event, event handler meth can handle it for allobjects whose class is class or a subclass of class, or that implementinterface ifac directly or through a superclass. If the event is a staticevent, event handler meth can handle it for class and its subclasses, orfor all classes that implement interface ifac.

    You can use the additions ABSTRACT and FINAL to make the event han-dler abstract or final, just like the general methods.

    The addition IMPORTING defines the input parameters of the event han-dler. pi can only be names of formal parameters that you defined asexport parameters of the event with addition EXPORTING for statement[CLASS-]EVENTS in the declaration of event evt in the class or inter-face ifac. The additions TYPE/LIKE and OPTIONAL/DEFAULT are notpossible. Any default parameters, the typing of the input parameters, andwhether or not the input parameters are optional are taken from the dec-laration of the event. You do not have to specify all the output parametersof the event.

    If evt is an instance event, you can also define a formal parameter calledsender as an input parameter of the event handler, in addition to itsexplicitly defined output parameters. sender is an implicit output param-eter of every instance event. It is fully typed as a reference variable thathas class or interface ifac as a static type, as defined in the declarationof the event handler after EVENT evt OF. If the event handler is called byan instance event, sender passes on a reference to the triggering object.

    Prior to Release 6.10, the static type of formal parameter sender wasdetermined by the class or interface in which the EVENTS statementdeclared the event. In Release 6.10 and later, the event handler deter-mines the type of its formal parameter itself.

    ExampleIn the example below, class picture contains an event handler handle_double_click for instance event picture_dblclick of global classcl_gui_picture. The event handler uses two explicit output parame-ters of the event, along with the implicit parameter sender, as inputparameters.

    KAP07.fm Seite 171 Freitag, 31. Januar 2003 2:28 14

  • 172 Defining Classes and Interfaces

    CLASS picture DEFINITION. PUBLIC SECTION. METHODS handle_double_click FOR EVENT picture_dblclick OF cl_gui_picture IMPORTING mouse_pos_x mouse_pos_y sender.ENDCLASS.

    CLASS picture IMPLEMENTATION. METHOD handle_double_click. ... ENDMETHOD.ENDCLASS.

    7.4.1.6 Redefining Instance Methods

    A method declared in a superclass can be redefined in a subclass, pro-vided it is not flagged as final in the superclass. Redefinition does notchange the methods interface.

    Syntax

    This statement is possible only in subclasses. It redefines an inheritedinstance method meth. It forces the redefinition of method meth in theimplementation section of the subclass. The new implementation in thecurrent class masks the implementation in the superclass. The redefinedmethod accesses the private components of the redefined class, not theprivate components with the same names in the superclass (if any).Within the redefined method, you can use the pseudo reference super->meth to call the implementation of the direct superclass.

    With the exception of the instance constructor, meth can be any non-final instance method that is declared in the public or protected visibilityarea of a superclass of the current class. In particular, meth can be anabstract method of an abstract superclass. The redefinition must takeplace within the same visibility area as the declaration of the method. Theinterface and type of the method (general or functional instance method,event handler) are not changed during a redefinition.

    The redefinition is valid for the subclasses of the redefined class until it isredefined again. You can continue to redefine a method along a path inthe inheritance tree until it is made final. If you use the addition FINAL

    METHODS meth [FINAL] REDEFINITION.

    KAP07.fm Seite 172 Freitag, 31. Januar 2003 2:28 14

  • Declaring Components in Classes and Interfaces 173

    during redefinition, the method is final from the current class and can nolonger be redefined in its subclasses.

    NoteEach object reference that points to an object in a subclass addresses theredefined methods regardless of its static type. This also applies to theself-reference me->.

    ExampleIn the example below, method m1 of superclass c1 is redefined in subclassc2. The original implementation is called with super->m1. Both methodsuse the private attribute a1 of the respective class. The redefined methodis executed with reference variable oref, which has static type c1 anddynamic 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.

    KAP07.fm Seite 173 Freitag, 31. Januar 2003 2:28 14

  • 174 Defining Classes and Interfaces

    ...

    DATA oref TYPE REF TO c1.

    CREATE OBJECT oref TYPE c2.oref->m1( `...` ).

    7.4.1.7 Static Methods

    The CLASS-METHODS statement declares static methods. You can usestatic methods with the class component selector (=>) independently ofobjects. In static methods, you can access only the static components of itsown class or its superclasses if you do not use a component selector.

    7.4.1.8 General Static Methods

    The most general form of the CLASS-METHODS statement lets you definestatic methods with any number of input and output parameters.

    Syntax

    This statement declares a general static method, meth. The additionsdefine the parameter interface of the method and define which excep-tions the method can propagate and/or trigger. The syntax and meaningof the additions are the same as for the general instance methods.

    NoteStatic methods cannot be redefined, and therefore cannot be madeabstract or final either.

    7.4.1.9 Functional Static Methods

    Functional methods can have any number of input parameters, and haveexactly one return value as output parameter.

    CLASS-METHODS

    CLASS-METHODS meth [IMPORTING {parameteri} [PREFERRED PARAMETER p]] [EXPORTING {parameteri}] [CHANGING {parameteri}] [{RAISING|EXCEPTIONS} {exci}].

    KAP07.fm Seite 174 Freitag, 31. Januar 2003 2:28 14

  • Declaring Components in Classes and Interfaces 175

    Syntax

    This statement declares a functional static method, meth. The additionshave exactly the same syntax and meaning as in the functional instancemethods.

    ExampleThe class described below, circle, contains two functional methods,circumference and area, and the constants pi.

    CLASS circle DEFINITION. PUBLIC SECTION. CONSTANTS pi TYPE f VALUE '3.14159265358979324'. CLASS-METHODS: circumference IMPORTING r TYPE f RETURNING value(c) TYPE f, area IMPORTING r TYPE f RETURNING value(a) TYPE f.ENDCLASS.

    CLASS circle IMPLEMENTATION. METHOD circumference. c = 2 * pi * r. ENDMETHOD. METHOD area. a = pi * r ** 2. ENDMETHOD.ENDCLASS.

    ...

    DATA: circ TYPE f, area TYPE f, radius TYPE f.

    radius = ...circ = circle=>circumference( radius ).area = circle=>area( radius ).

    CLASS-METHODS meth [IMPORTING {parameteri}] RETURNING VALUE(r) {TYPE type_spec}|{LIKE dobj_spec} [{RAISING|EXCEPTIONS} {exci}].

    KAP07.fm Seite 175 Freitag, 31. Januar 2003 2:28 14

  • 176 Defining Classes and Interfaces

    7.4.1.10 Static Constructors

    Static constructors are methods with the predefined name class_con-structor, which are called automatically before the first use of theirclass. Static constructors do not have a parameter interface.

    Syntax

    This statement is possible only in the public visibility area of the declara-tion section of a class. It declares the static constructor of the class,class_constructor.

    Each class has a predefined method called class_constructor in itspublic visibility area. The explicit declaration enables you to implement itsfunctionality class-specifically. If you do not declare it explicitly, the staticconstructor is blank.

    The static constructor is executed once for each class and internal session.This execution takes place automatically before the first access to theclass. You access a class whenever you generate an instance of that classor address a static component with the class component selector.

    During the first access of a subclass, the system searches for the next-highest superclass in the inheritance tree whose static constructor has notbeen executed yet. The static constructor of the superclass is then exe-cuted, followed successively by each subsequent subclass down to theaddressed subclass.

    Like all static methods, the static constructor can access only the staticcomponents of its class. Furthermore, the static constructor cannotaddress 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 inherit-ance 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 theclass. The static constructor is usually executed immediately prior tothe access of the class. The static constructor is called at the start of therespective processing block only if a static attribute of the class isaccessed. We do not recommend writing programs that require thestatic constructor to be executed at a specific time.

    CLASS-METHODS class_constructor.

    KAP07.fm Seite 176 Freitag, 31. Januar 2003 2:28 14

  • Declaring Components in Classes and Interfaces 177

    ExampleThe static constructor of the class below sets static attribute access_program to the name of the program of the first internal session that usesa class during the first access to that class.

    CLASS some_class DEFINITION. PUBLIC SECTION. CLASS-METHODS class_constructor. PRIVATE SECTION. CLASS-DATA access_program TYPE sy-repid.ENDCLASS.

    CLASS some_class IMPLEMENTATION. METHOD class_constructor. access_program = sy-repid. ENDMETHOD.ENDCLASS.

    7.4.1.11 Static Event Handlers

    Static event handlers are static methods that are called by an event of aclass or an interface. The only possible formal parameters of an event han-dler are input parameters that are defined as output parameters of theevent.

    This statement declares the static method meth as the event handler evtof class or interface ifac. The syntax and meaning of the additions areidentical to those in the declaration of instance methods as event han-dlers.

    Static event handlers can be called by event evt independently of aninstance of a class.

    ExampleIn the class described below, dialog_box, a static event handler close_box is defined for the event. This event is triggered when the userattempts to close a dialog box of the GUI control framework (CFW).

    CLASS-METHODS meth FOR EVENT evt OF {class|ifac} [IMPORTING {pi} [sender]].

    KAP07.fm Seite 177 Freitag, 31. Januar 2003 2:28 14

  • 178 Defining Classes and Interfaces

    CLASS dialog_box DEFINITION. PUBLIC SECTION. METHODS constructor. ... PRIVATE SECTION. CLASS-DATA open_boxes TYPE i. CLASS-METHODS close_box FOR EVENT close OF cl_gui_dialogbox_container IMPORTING sender. ...ENDCLASS.

    CLASS dialog_box IMPLEMENTATION. METHOD constructor. ... " create a dialogbox open_boxes = open_boxes + 1. ENDMETHOD. METHOD close_box ... " close the dialogbox referred by sender open_boxes = open_boxes - 1. ENDMETHOD.ENDCLASS.

    7.4.2 Events

    You use the EVENTS and CLASS-EVENTS statements to declare events.When you declare an event in a class, the methods of that class can trig-ger the event, which in turn triggers the execution of event handlers. Dur-ing the declaration, you can define output parameters for an event thatare passed on to the event handler as actual parameters when the event istriggered.

    7.4.2.1 Instance Events

    The EVENTS statement declares instance events. Instance events are tiedto objects. They can be triggered only in instance methods of the sameclass.

    EVENTS

    KAP07.fm Seite 178 Freitag, 31. Januar 2003 2:28 14

  • Declaring Components in Classes and Interfaces 179

    Syntax

    This statement declares an instance event, evt, in a class or an interface.evt can be triggered with the RAISE EVENT statement in all instancemethods of the same class or in any class that implements the interface,as well asif visible therein the instance methods of subclasses.

    The addition EXPORTING defines the parameter interface of event evt.An event has only output parameters, pi, which are passed on as values.The syntax of the additions VALUE, OPTIONAL, and DEFAULT and thetyping correspond to the definition of formal parameters in methodinterfaces with the METHODS statement.

    When you trigger an event with the RAISE EVENT statement, you have tospecify suitable actual parameters for all non-optional output parameters.You can also specify any desired optional parameters. Optional parame-ters for which no actual parameter is specified are set to their type-spe-cific initial value or to the default parameter, defi.

    When you declare an event handler with the addition FOR EVENT OF inthe [CLASS-]METHODS statement, you can define the output parametersof the event as the input parameters of the event handler; in this case, theattributes of the input parameters are copied to the output parametersdefined in EVENTS.

    In addition to the output parameters that you define with EXPORTING,each instance event also has an implicit output parameter, sender. Thisoutput parameter has the type of a reference variable. When you triggeran event with the RAISE EVENT statement, sender is implicitly assignedthe reference to the triggering object.

    Prior to Release 6.10, the static type of implicit parameter sender wasdefined by the class or interface where the EVENTS statement was listed.In Release 6.10 and later, the static type of input parameter sender isdefined by the addition FOR EVENT OF in the [CLASS-]METHODS state-ment.

    NoteThe dynamic type of the implicit formal parameter, sender, is always theclass of the object in which the event is triggered.

    EVENTS evt [EXPORTING { VALUE(pi) typing [OPTIONAL|{DEFAULT defi}] } ].

    KAP07.fm Seite 179 Freitag, 31. Januar 2003 2:28 14

  • 180 Defining Classes and Interfaces

    ExampleIn the interface below, window, three events are declared, each with anexplicit, non-optional output parameter, status. Class dialog_windowimplements interface window. Interface window_handler contains eventhandlers that import both the explicit parameter, status, and theimplicit parameter, sender. The static type of input parameter sender isclass dialog_window. Prior to Release 6.10, interface window is the statictype.

    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

    The CLASS-EVENTS statement declares static events. Static events are nottied to objects. They can be triggered in all methods of the same class.

    Syntax

    CLASS-EVENTS

    CLASS-EVENTS evt [EXPORTING { VALUE(pi) typing [OPTIONAL|{DEFAULT defi}] } ].

    KAP07.fm Seite 180 Freitag, 31. Januar 2003 2:28 14

  • Declaring Components in Classes and Interfaces 181

    This statement declares a static event, evt, in a class or an interface. evtcan be triggered with the RAISE EVENT statement in all methods of thesame class and in any class that implements the interface, as well asifvisible therein the methods of subclasses.

    The addition EXPORTING defines the parameter interface of event evt.The syntax and meaning of the additions VALUE, OPTIONAL, and DEFAULTand the typing correspond to the definition of instance events with theEVENTS statement.

    NoteStatic events do not have an implicit formal parameter called sender.

    7.4.3 Implementing and Integrating Interfaces

    Interfaces can be implemented by classes or integrated by other inter-faces. You can define alias names for interface components.

    The INTERFACES statement is possible in the public visibility area of thedeclaration section of classes and in interface declarations.

    7.4.3.1 Implementing Interfaces in Classes

    Syntax

    In the public visibility area of a class, the INTERFACES statement imple-ments interface ifac in the class. You can also specify additions, whichdefine the properties of interface components in the class.

    ifac can be any local or global interfaces that are visible at this point. Theimplementation makes the components of the interfaces public compo-nents of the class. An interface component named comp has the nameifac~comp in the class: ifac is the name of the interface and the ~ char-acter is the interface component selector. A class has to implement allmethods of the interface in its implementation section, provided it is notdeclared as abstract.

    INTERFACES

    INTERFACES ifac { { [ABSTRACT METHODS {methi}] [FINAL METHODS {methj}] } | [ALL METHODS {ABSTRACT|FINAL}] } [DATA VALUES {attri = vali}].

    KAP07.fm Seite 181 Freitag, 31. Januar 2003 2:28 14

  • 182 Defining Classes and Interfaces

    You can use the additions ABSTRACT METHODS and FINAL METHODS tomake individual instance methods of the interface, methi, abstract or finalin the implementing class. The same rules apply as for the additionsABSTRACT and FINAL of the METHODS statement. In particular, when youmake an interface method abstract, the entire class has to be abstract,and you cannot list an interface method after both ABSTRACT METHODSand FINAL METHODS at the same time. Instead of making individualinstance methods abstract or final in the class, you can use the additionALL METHODS {ABSTRACT|FINAL} to make all the interface methodseither abst