Visit the SAP Forum

Languages : English | Dutch | French | Portugese | Italian



INTERVIEW EBOOK
Get 30,000 Interview Questions & Answers in an eBook.

Interview Success Kit - Get Success in Job Interviews



Send your Resume to 6000 Companies
START YOUR WEBSITE
India's Best Web Hosting Company

Object Declaring and Calling Methods


Home »ABAP OBJECT » Object Declaring and Calling Methods


Object Declaring and Calling Methods

This section contains explains how to work with methods in ABAP Objects. For precise details of the relevant ABAP statements, refer to the corresponding keyword documentation in the ABAP Editor.

Declaring Methods

You can declare methods in the declaration part of a class or in an interface. To declare instance methods, use the following statement:

METHODS meth IMPORTING [VALUE(]i1 i2 � [)] TYPE type [OPTIONAL]�
EXPORTING [VALUE(]
e1 e2 � [)] TYPE type �
CHANGING [VALUE(]
c1 c2 � [)] TYPE type [OPTIONAL]�
RETURNING VALUE(r)

EXCEPTIONS
exc1 exc2 � .

and the appropriate additions.

To declare static methods, use the following statement:

CLASS-METHODS meth�

Both statements have the same syntax.

When you declare a method, you also define its parameter interface using the additions IMPORTING, EXPORTING, CHANGING, and RETURNING. The additions define the input, output, and input/output parameters, and the return code. They also define the attributes of the interface parameters, namely whether a parameter is to be passed by reference or value (VALUE), its type (TYPE), and whether it is optional (OPTIONAL, DEFAULT). Unlike in function modules, the default way of passing a parameter in a method is by reference. To pass a parameter by value, you must do so explicitly using the VALUE addition. The return value (RETURNING parameter) must always be passed explicitly as a value. This is suitable for methods that return a single output value. If you use it, you cannot use EXPORTING or CHANGING parameters.

As in function modules, you can use exception parameters (EXCEPTIONS) to allow the user to react to error situations when the method is executed.

Implementing Methods

You must implement all of the methods in a class in the implementation part of the class in a

METHOD meth.

ENDMETHOD.

block. When you implement the method, you do not have to specify any interface parameters, since these are defined in the method declaration. The interface parameters of a method behave like local variables within the method implementation. You can define additional local variables within a method using the DATA statement.

As in function modules, you can use the RAISE exception and MESSAGE RAISING statements to handle error situations.

When you implement a static method, remember that it can only work with the static attributes of your class. Instance methods can work with both static and instance attributes.

Static Method Call

To call a method, use the following statement:

CALL METHOD meth EXPORTING i1 = f1 i2 =f2

IMPORTING e1 = g1 e2 =g2
CHANGING c1 = f1 c2 =f2
RECEIVING r = h
EXCEPTIONS e1 = rc1 e2 =rc2

The way in which you address the method method depends on the method itself and from where you are calling it. Within the implementation part of a class, you can call the methods of the same class directly using their name meth.

CALL METHOD meth�

Outside the class, the visibility of the method depends on whether you can call it at all. Visible instance methods can be called from outside the class using

CALL METHOD ref->meth�

where ref is a reference variable whose value points to an instance of the class. Visible instance methods can be called from outside the class using

CALL METHOD =>meth�

where class is the name of the relevant class.

When you call a method, you must pass all non-optional input parameters using the EXPORTING or CHANGING addition in the CALL METHOD statement. You can (but do not have to) import the output parameters into your program using the IMPORTING or RECEIVING addition. Equally, you can (but do not have to) handle any exceptions triggered by the exceptions using the EXCEPTIONS addition. However, this is recommended.

You pass and receive values to and from methods in the same way as with function modules, that is, with the syntax:

� Formal parameter = Actual parameter

after the corresponding addition. The interface parameters (formal parameters) are always on the left-hand side of the equals sign. The actual parameters are always on the right. The equals sign is not an assignment operator in this context; it merely serves to assign program variables to the interface parameters of the method.

If the interface of a method consists only of a single IMPORTING parameter, you can use the following shortened form of the method call:

CALL METHOD method( f ).

The actual parameter f is passed to the input parameters of the method.

If the interface of a method consists only of IMPORTING parameters, you can use the following shortened form of the method call:

CALL METHOD method( i1 = f1 i2 = f2 �).

Each actual parameter f1 is passed to the corresponding formal parameter i1.

Dynamic Method Call

Using the standard ABAP parenthesis semantics you can call methods dynamically.

Calling an instance method meth:

CALL METHOD ref->(f)

Calling a static method meth:

CALL METHOD class=>(f)
CALL METHOD (c)=>meth
CALL METHOD (c)=>(f)

Calling a user-defined method meth:

CALL METHOD (f)
CALL METHOD ME->(f)

Here, f and c are fields containing the name of the method meth or of the class class. Unlike subroutines and function modules, a dynamic method call allows you to pass the actual parameters dynamically as well. To do this, you use the additions PARAMETER-TABLE and EXCEPTION-TABLE of the CALL METHOD statement:

CALL METHOD � PARAMETER-TABLE ptab
EXCEPTION-TABLE etab.

The parameter table ptab must be a hash table of the table type ABAP_PARMBIND_TAB or of the line type ABAP_PARMBIND. These types are defined in the ABAP Dictionary. The table has the following three columns:

NAME for the name of the formal parameter

KIND for the type of parameter passing

VALUE of the type REF TO DATA for the value of the actual parameter

The NAME column is the unique table key. For each non-optional parameter you must fill exactly one line of the internal table. For each optional parameter you can do this, but are not forced to.

The type of parameter passing is determined in the declaration of the method called for each formal parameter. The contents of the KIND column may therefore be initial. If you want to check the type of parameter passing at runtime, you can assign one of the following constants from the global class CL_ABAP_OBJECTDESCR to the KIND column:

CL_ABAP_OBJECTDESCR=>EXPORTING for EXPORTING parameters

CL_ABAP_OBJECTDESCR=>IMPORTING for IMPORTING parameters

CL_ABAP_OBJECTDESCR=>CHANGING for CHANGING parameters

CL_ABAP_OBJECTDESCR=>RECEIVING for RECEIVING parameters

The descriptions depend on the view of the caller. If the specified and actual parameter types differ, the system generates the catchable runtime error DYN_CALL_METH_PARAM_KIND. You can also use the entries in the KIND column as an additional key, for example, to read all imported values after the method call. For the value of the actual parameter, the VALUE reference of the table line must point to a data object containing the requested value.
 

The exception table etab must be a hash table of the table type ABAP_EXCPBIND_TAB or of the line type ABAP_EXCPBIND. These types are defined in the ABAP Dictionary. The table has the following two columns:

NAME for the name of the exception

VALUE of type i for the value to be assigned to sy-subrc

The NAME column is the unique table key. For each exception, you can fill exactly one line of the internal table. The VALUE component is assigned the numeric value to appear in sy-subrc after the exception is triggered.

Functional Methods

Functional methods are methods with any number of IMPORTING parameters and one RETURNING parameter. In addition to CALL METHOD you can also use the following expressions at operand positions to call functional methods:

A D V E R T I S E M E N T

S A P - R E S O U R C E S


Get Free SAP Resources:

Are you looking for:
  • SAP Jobs
  • Free SAP eBooks
  • SAP Softwares
  • SAP Tutorials
  • ERP Implementation Examples
Enter Email Address:
(Enter your email address above and we will instantly send you the download link to you, when you confirm your email.)
Search SAP Resources:

IMPORTING parameters

Expression

None

meth( )

One

meth( f ) oder meth( p = f )

n

meth( p1 = f1 � pn = fn )

This notation is currently supported:

for the source field of the MOVE statement

in arithmetic expressions of the COMPUTE statement

in logical expressions

in the CASE statement of the CASE control structure

in the WHEN statement of the CASE control structure

in the WHERE condition of the statements LOOP AT, DELETE and MODIFYfor internal tables

The functional method is entered instead of an operand. When the statement is executed, the method is called and the RETURNING parameter passed back is used as the operand.

Event Handler Methods

Event handler methods are special methods that cannot all be called using the CALL METHOD statement. Instead, they are triggered using events. You define a method as an event handler method using the addition

� FOR EVENT evt OF cif �

in the METHODS or CLASS-METHODS statement.

The following special rules apply to the interface of an event handler method:

The interface may only consist of IMPORTING parameters.

Each IMPORTING parameter must be an EXPORTING parameter of the event evt.

The attributes of the parameters are defined in the declaration of the event evt (EVENTS statement) and are adopted by the event handler method.

Constructors

Constructors are special methods that cannot be called using CALL METHOD. Instead, they are called automatically by the system to set the initial state of a new object or class. There are two types of constructors - instance constructors and static constructors. Constructors are methods with a predefined name. To use them, you must declare them explicitly in the class.

The instance constructor of a class is the predefined instance method CONSTRUCTOR. You declare it in the public section as follows:

METHODS CONSTRUCTOR
IMPORTING [VALUE(]
i1 i2 �[)] TYPE type [OPTIONAL]�
EXCEPTIONS
exc1 exc2 � .

and implement it in the implementation section like any other method. The system calls the instance constructor once for each instance of the class, directly after the object has been created in the CREATE OBJECT statement. You can pass the input parameters of the instance constructor and handle its exceptions using the EXPORTING and EXCEPTIONS additions in the CREATE OBJECT statement.

The static constructor of a class is the predefined static method CLASS_CONSTRUCTOR. You declare it in the public section as follows:

CLASS-METHODS CLASS_CONSTRUCTOR.

and implement it in the implementation section like any other method. The static constructor has no parameters. The system calls the static constructor once for each class, before the class is accessed for the first time. The static constructor cannot therefore access the components of its own class.

A D V E R T I S E M E N T

discussionDiscussion Center
Discuss
Discuss

Query

Feedback
Yahoo Groups
Y! Group
Sirfdosti Groups
Sirfdosti
Contact Us
Contact
Sign in

User Name:
Password:
Forget password? | Register


INTERVIEW EBOOK

Get 30,000 Interview Questions & Answers in an eBook.

Interview Success Kit - Get Success in Job Interviews


Send your Resume to 6000 Companies

SAP Resources

  • SAP Articles
  • SAP Books
  • SAP Certifications
  • SAP Companies
  • SAP Study Materials
  • SAP Events
  • SAP Faqs
  • SAP Jobs
  • SAP Job Consultants
  • SAP Links
  • SAP News
  • SAP Sample Papers
  • SAP Interview Questions
  • SAP Training Institutes
  •  
    SAP Tutorial

  • Introduction
  • SAP History
  • SAP Facts
  • SAP Tables
  • SAP Function
  • SAP Report
  • Reasons To Use SAP
  • SAP Landscape
  • SAP Transaction Code
  • SAP Lotus Notes Integration
  • Business Connector and XML
  • Enterprise Release 4.7

  • SAP Training Institutes

  • SAP Training Institutes
  • Job Consultants Address
  • SAP Companies Address

  • Business Process Cycle

  • Analyze
  • Design
  • Implement
  • Operate
  • Optimize
  • Skills And Education

  • Logistics Module

  • Sales and Distribution
  • Quality Management
  • Materail Management
  • Plant Maintenance
  • Financial Modules (FI/CO)
  • Project system
  • Production Planning
  • Service Management

  • SAP NetWeaver

  • Introduction
  • Object Model
  • Interoperability
  • Creating Web Apps
  • Java Connector
  • Advantages & Conclusion
  • Life Cycle Management
  • Composite Application Framework

  • SAP Tools

  • SQL Trace Tool
  • SAP CRM
  • SAP SRM
  • SAP SCM
  • SAP ERP
  • SAP BW

  • SAP Modules

  • SAP FI Module
  • SAP CO Module

  • SAP Technical

  • SAP ITS Architecture
  • Financial Management
  • Manufacturing Planning and Execution
  • Customer Order Management
  • Lackbox Processing
  • SD and Financial Accounting Integration
  • SAP - HR (Human Resource)

  • SAP Miscellanous

  • Web Application Server(WAS)
  • Data Archiving
  • Mobile Engine
  • SAP Script
  • SAP Transactions
  • SAP Business Warehouse(BW)

  • SAP Reference

  • SAP Facts
  • SAP Tables
  • SAP Function
  • SAP Report
  • SAP Landscape

  • SAP Others

  • SAP Treasury
  • SAP Finance
  • SAP WAS
  • SAP Data Archiving
  • SAP ALE
  • SAP EDI
  • SAP Exchange
  • SAP X-APPS
  • SAP Applications Components
  • SAP Software
  • Dunning
  • SAP IDES
  • SAP Payroll Basics
  • SAP BASIS and Security Authorization
  • SAP Backup
  • SAP Router

  • ABAP Tutorials

  • Introduction
  • ABAP Transaction
  • ABAP Function
  • ABAP File Processing
  • ABAP Objects
  • ABAP Syntax
  • ABAP Queries
  • SAPMail Using ABAP
  • ABAP Programming Hints

  • SAP Projects & Codes

  • Finding the user-exits
  • Purchase Order Display
  • Batch Input
  • Dynamic Open SQL
  • Creating Extract Dataset
  • Reading database tables
  • Load table from a UNIX file
  • Create subscreen

  • SAP Resources

  • SAP Books
  • SAP Links

  • Interview Question



  • Common Interview Que.
  • ABAP Interview Que.
  • ERP Interview Que.
  • HR Interview Que.
  • MM Interview Que.
  • PP Interview Que.
  • ALE Interview Que.
  • EDI Interview Que.
  • Basis Interview Questions
  • SAP-SD Interview Questions
  • FI&CO Interview Questions
  • BW Interview Questions
  • CRM Interview Questions
  • Interview Questions

  • Careers at SAP

  • Careers in SAP (SAP Jobs)
  • Advertise
  •