What are Boxed Components in ABAP?



While developing programs we give very less attention to how the memory for this component will be allocated and what could happen if the data for the component will grow exponentially?
Here is an example that we do in most of the cases:
Suppose you have a type for a customer address as below:


TYPES:BEGIN OF address,

...
END OF address.

Now let’s say you are capturing the address details in an order.


TYPES:BEGIN OF order,
...
post_addrTYPE address,
alt_post_addrTYPE address, "alternative address
END OF order.

So the memory structure for this will be something like this:
Now if there is no alternate address for your 1000 customers out of 1200 then this space will be wasted for 1000 records!!!
A more memory savvy solution to this could be using references. But that we have to keep record of if the reference is created and assigned properly. Means headache to developer.

TYPES:BEGIN OF order,
...
alt_post_addrTYPE REF TO address,
END OF order.

The memory organization will be then as below:
Now to solve this types of problems SAP has come up with the concept of shared initial record or Memory allocation on demand.
You define the component BOXED and SAP memory organizer will take care of the allocation.

TYPES:BEGIN OF order,
...
post_addrTYPE address,
alt_post_addrTYPE address BOXED,
END OF order.

The initial memory allocation will be as below:

When a new record with an alternate post address is found the memory organizer will take care of it and allocate dynamically.


This concept can also be used in Object oriented ABAP also. We can define the components of class to be BOXED.

CLASS lcl_orderDEFINITION.
PUBLIC SECTION.
...
DATA: post_addrTYPE address,
alt_post_addrTYPE address BOXED.
ENDCLASS.