Note: This user guide is no longer maintained. For information about any updates that relate to this guide after service refresh 4, fix pack 25, see the following support document: IBM SDK, Java Technology Edition, Version 7 Release 1: Current news
Nested fields
When a packed class contains an instance field that is a packed type, the data for that field is packed directly into the containing class. The field is known as a nested field. When reading from a nested field, a small object is created as a pointer to the data. When writing to a nested field, you must use an explicit copy operation.
A nested packed array is a type of nested field. For more information, see Nested packed arrays.
Consider a class that represents a simple two dimensional box that you might use in a graphical user interface or drawing program. The class consists of two points: the start point and the dimensions of the box. The class also has two fields that represent the colors to use for the frame of the box, and the interior fill.
Here are some examples:
package com.ibm.packedexample.concepts;
@Packed
public final class Point extends PackedObject
{
public int x;
public int y;
}
Box.java:package com.ibm.packedexample.concepts;
@ImportPacked({
"com/ibm/packedexample/concepts/RGBColour",
"com/ibm/packedexample/concepts/Point",
})
@Packed
public final class Box extends PackedObject
{
public Point origin;
public Point extent;
public RGBColour frameColour;
public RGBColour fillColour;
}


You do not need to allocate and initialize nested fields. Rather than being initially null, all the data is available and initialized to the default value. See Initialization and construction.
Reading nested fields
When accessing a nested field in a packed object, the JVM must return an object for further operations to interact with. Because the data for the field is contained within the object, a small object must be generated to describe the data. This object is known as a derived object. This object is not a copy of the data, but a pointer to the data.

box.extent.y
The derived object for the box.extent fields can be removed by compiler optimization because the appropriate
offset into the object data can be calculated directly. In the example,
this offset is offset 12 into the data.Writing to nested fields
Because nested fields consist of data and not object pointers, you cannot assign an object into a nested field. The data can be copied in, but the reference to the object cannot be preserved. Likewise, further changes to the stored object are not reflected in the field, as these changes would be if storing a reference. Therefore, direct assignment into nested fields is not allowed. Instead, you must use an explicit copy operation with the PackedObject.copyFrom(PackedObject) method. This method copies the data from the argument into the receiver.
void setFillColour(RGBColour value)
{
fillColour = value; // Invalid
}
void setFillColour(RGBColour value)
{
fillColour.copyFrom(value);
}
For more information about assignment to nested fields, see Assignment Semantics.
- The compiler expects the field to be initialized, that is, assigned to, before the constructor completes.
- The JVM has implicitly initialized the field along with the containing object, and prevents assignment to the field.