Professional Documents
Culture Documents
Document: Object Handling URL: http://help.sap.com/saphelp_nw73ehp1/helpdata/en/c3/225b5f54f411d194a60000e8353423/content.htm Date created: August 07, 2013
2013 SAP AG or an SAP affiliate company. All rights reserved. No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG. The information contained herein may be changed without prior notice. Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors. National product specifications may vary. These materials are provided by SAP AG and its affiliated companies ("SAP Group") for informational purposes only, without representation or warranty of any kind, and SAP Group shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP Group products and services are those that are set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty. SAP and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP AG in Germany and other countries. Please see www.sap.com/corporate-en/legal/copyright/index.epx#trademark for additional trademark information and notices.
Note This PDF document contains the selected topic and its subtopics (max. 150) in the selected structure. Subtopics from other structures are not included.
Page 1 of 4
Object Handling
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 only object references. Object references are pointers to objects. In ABAP, they are always contained in object reference variables.
Creating Objects
Now that you defined a class reference variable crefwith reference to a class class , you can now create an instance (object) of the class. CREATE OBJECT cref [TYPE class]. This statement creates an instance of the class class , and the reference variable crefcontains a reference to the object. You do not need the TYPE class addition in this case. This addition is only important in the following two situations: 1. if you use inheritance and want to create an instance of a class class with a class reference variable cref which does not have the type of the class class , or 2. if you use interfaces and want to create an instance of a class class with an interface reference variable iref. After TYPE you can specify the class dynamically as the contents of a field using the standard parenthesis syntax.
Page 2 of 4
Casting
Whenever a static type check is not possible or when the type checks are to be performed at program runtime, you must use the statement MOVE ... ?TO ... or the casting operator (?= ). The casting assignment replaces the assignment operator (= ). In the MOVE... ? TO statement, or when you use the casting assignment, there is no static type check. Instead, the system checks at runtime whether the object reference in the source variable points to an object to which the object reference in the target variable can also point. If the assignment is possible, the system makes it, otherwise, the catchable runtime error MOVE_CAST_ERROR occurs. Syntax rules force you to use casting whenever a static type check is not possible, for example: cref1 ?= iref1. Here, an interface reference is assigned to a class reference. For the casting to be successful, the object to which irefr points must be an object of the same class as the class of the class variable cref1 or one of its subclasses..
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
Page 3 of 4
The above illustration shows a class C1 on the left, with its instances represented in the internal session of an ABAP program on the right. To distinguish them from classes, instances are drawn with rounded corners. The instance names above use the same notation as is used for reference variables in the Debugger.
Page 4 of 4