Nested Structures

A structure can embed another structure simply by naming the other structure as the field type. For example, the Windows MSG structure embeds a POINT structure as follows:

   typedef struct {
      LONG x;
      LONG y;
   } POINT;

   typedef struct {
      int hwnd;
      int message;
      int wParam;
      int lParam;
      int time;
      POINT pt;
   } MSG;

This translates directly into Java as follows:

   /** @dll.struct() */
   class POINT {
      int x;
      int y;
   }
   /** @dll.struct() */
   class MSG {
      public int hwnd;
      public int message;
      public int wParam;
      public int lParam;
      public int time;
      public POINT pt;
   }

Tip   Although embedding structures is handy, the fact remains that Java does not truly support embedded objects – only embedded references to objects. The Microsoft VM must translate between these two formats each time a nested structure is passed. Therefore, in a critical code path, you can improve performance by nesting structures manually (by copying the fields of the nested structure into the containing structure). For example, the pt field in the MSG structure could easily be declared as two integer fields, pt_x and pt_y.