Skip to content

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

Creating a Class Instance as a Shared Object

The 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.
        MESSAGE exc TYPE 'I' DISPLAY LIKE 'E'.
        LEAVE PROGRAM.
    ENDTRY.

    TRY.
        handle = cl_demo_area=>attach_for_read( ).
        oref ?= handle->root->oref.
        MESSAGE oref->attr TYPE 'I'.
        CLEAR oref.
        handle->detach( ).
      CATCH cx_shm_attach_error INTO exc.
        MESSAGE exc TYPE 'I' DISPLAY LIKE 'E'.
        LEAVE PROGRAM.
    ENDTRY.

Description

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

Once write access is completed using the DETACH_COMMIT method, read access takes place, which demonstrates how the object is accessed in the shared memory. This type of access can also take place in a different program, provided that the area instance version exists in the shared memory.