Entering content frameObject Handling Locate the document in its SAP Library structure

Objects

Objects are instances of classes. Each object has a unique identity and its own attributes. All transient objects reside in the context of an internal session (memory area of an ABAP program). Persistent objects in the database are not yet available. A class can have any number of objects (instances).

Object References

To access an object from an ABAP program, you use object references. Object references are pointers to objects. In ABAP, they are always contained in reference variables.

Reference variables

Reference variables contain references. A reference variable is either initial or contains a reference to an existing object. The identity of an object depends on its reference. A reference variable that points to an object knows the identity of that object. Users cannot access the identity of the object directly.

Reference variables in ABAP are treated like other elementary data objects. This means that a reference variable can occur as a component of a structure or internal table as well as on its own.

Data Types for References

ABAP contains a predefined data type for references, comparable to the data types for structures or internal tables. The full data type is not defined until the declaration in the ABAP program. The data type of a reference variable determines how the program handles its value (that is, the object reference). There are two principal types of references: Class references and interface references (see Interfaces).

You define class references using the

... TYPE REF TO <class>

addition in the TYPES or DATA statement, where <class> refers to a class. A reference variable with the type class reference is called a class reference variable, or class reference for short.

A class reference <cref> allows a user to create an instance (object) of the corresponding class, and to address a visible component <comp> within it using the form

cref->comp

Creating Objects

Before you can create an object for a class, you need to declare a reference variable with reference to that class. Once you have declared a class reference variable <obj> for a class <class>, you can create an object using the statement

CREATE OBJECT <cref>.

This statement creates an instance of the class <class>, and the reference variable <cref> contains a reference to the object.

Addressing the Components of Objects

Programs can only access the instance components of an object using references in reference variables. The syntax is as follows (where <ref> is a reference variable):

You can access static components using the class name as well as the reference variable. It is also possible to address the static components of a class before an object has been created.

Within a class, you can use the self-reference ME to access the individual components:

Self references allow an object to give other objects a reference to it. You can also access attributes in methods from within an object even if they are obscured by local attributes of the method.

Creating More Than One Instance of a Class

In a program, you can create any number of objects from the same class. The objects are fully independent of each other. Each one has its own identity within the program and its own attributes. Each CREATE OBJECT statement generates a new object, whose identity is defined by its unique object reference.

Assigning References

You can assign references to different reference variables using the MOVE statement. In this way, you can make the references in several reference variables point to the same object. When you assign a reference to a different reference variable, their types must be either compatible or convertible.

When you use the MOVE statement or the assignment operator (=) to assign reference variables, the system must be able to recognize in the syntax check whether an assignment is possible. The same applies when you pass reference variables to procedures as parameters. If you write the statement

<cref1> = <cref2>

the two class references <cref1> and <cref2> must have the same type, that is, they must either refer to the same class, or the class of <cref1> must be the predefined empty class OBJECT. The class OBJECT has no components, and has the same function for reference variables as the data type ANY has for normal variables. Reference variables with the type OBJECT can function as containers for passing references. However, you cannot use them to address objects.

Object Lifetime

An object exists for as long as it is being used in the program. An object is in use by a program for as long as at least one reference points to it, or at least one method of the object is registered as an event handler.

As soon as there are no more references to an object, and so long as none of its methods are registered as event handlers, it is deleted by the automatic memory management (garbage collection). The ID of the object then becomes free, and can be used by a new object.

 

See also:

Overview Graphic

Example

 

 

 

 

Leaving content frame