Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Creating Objects and Values →  CREATE OBJECT →  CREATE OBJECT - AREA HANDLE 

Creating an Instance of a Class as a Shared Object

This example demonstrates how an object is created in an area instance version.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA: handle TYPE REF TO cl_demo_area,
          root   TYPE REF TO cl_demo_root,
          exc    TYPE REF TO cx_shm_attach_error,
          oref   TYPE REF TO class.

    TRY.
        handle = cl_demo_area=>attach_for_write( ).
        CREATE OBJECT root AREA HANDLE handle.
        handle->set_root( root ).
        CREATE OBJECT root->oref AREA HANDLE handle TYPE class.
        oref ?= root->oref.
        oref->set_attr( `String in shared memory` ).
        CLEAR oref.
        handle->detach_commit( ).
      CATCH cx_shm_attach_error INTO exc.
        cl_demo_output=>display_text( exc->get_text( ) ).
        LEAVE PROGRAM.
    ENDTRY.

    TRY.
        handle = cl_demo_area=>attach_for_read( ).
        oref ?= handle->root->oref.
        cl_demo_output=>display_data( oref->attr ).
        CLEAR oref.
        handle->detach( ).
      CATCH cx_shm_attach_error INTO exc.
        cl_demo_output=>display_text( exc->get_text( ) ).
        LEAVE PROGRAM.
    ENDTRY.

Description

The addition AREA HANDLE is used to create an instance of the local class class as a shared object in an area instance version of the area CL_DEMO_AREA. The generically typed attribute oref of the area root class CL_DEMO_ROOT is used a a reference variable. The attribute attr of the object is given a value by calling its method set_attr.

Once the method DETACH_COMMIT completes the write, a read is performed to demonstrate how the objects in the shared memory are accessed. An access of this type can also be made in another program, as long as the area instance version exists in the shared memory.