In Java you declare an array and then you call new to allocate space. so that a class with 4 named integers takes less space and has better locality than an array of size 4.
Is there any way to have an array of 4 elements, but have it allocated the same way as named vaiables a1, a2, a3, a4
For those that know C++, this is the same as asking for int x[4] as opposed to int *x
class X
{
int x1;
int x2;
int x3;
int x4;
};
[ Class OID ][x1][x2][x3][x4] = 1 ref + 4 int
class Y
{
int y[];
};
y=new int[4];
[ Class OID ][Y] ======> [Array OID][Array Size][y][y][y][y] = 3 ref + size + 4 int
intis 4 bytes, whether in an array or in a regular object. Both array and regular object must contain the class pointer. Now, if you're talking of embedding one object (whether array or regular object) in another, instead of using a pointer ("reference") to refer to the object, that can't be done. If you want to do that use C# (though I'm not sure that C# can really do it vs simply simulate it).