If I am understanding the question correctly, then it is something you could have easily implemented yourself, but here we go:
public class Test {
private int a1;
private long a2;
private double a3[];
public Test(final int a1, final long a2, final double[] a3) {
this.a1 = a1;
this.a2 = a2;
this.a3 = a3;
}
public int getSize() {
return 4 + 8 + (a3.length * 8);
}
}
Also a few notes:
- Refrain from using
Test
as classname, you should use something likeSizeTest
or whatever, but simply test will eventually cause havoc if you would decide to unit test it or whatnot. - Give variables meaningful names. If I had the oppurtunity I would've done so, but I can really cannot make anything from the current names.
If you are looking for a generic function, then well things turn a lot harder, in that case you would be looking for a method with the followed signature: SizeTester.getSize(Object object);
, it is still doable, but you would need to use Reflection then to get information about the fields and you should only use that if absolutely neccessary.