0

I have a java class which contains three integers A, B and C. I use this class on both, a client and a server application. But I never access A on the server side. Would Java even allocate memory for A when I create an object of that class on the server?

Please notice, that it´s not a transfer object. I just use the class in both applications to avoid duplicate code.

Franz Deschler
  • 2,456
  • 5
  • 25
  • 39

2 Answers2

3

Yes memory would be allocated for the integer members -- it has nothing to do with whether they are accessed in your code or not. So for the following:

class MyClass {

   int a;
   int b;
   int c;

   ...
}

The members are initialized to zero by default. Even if the field type is the reference type Integer, the default value is null but the reference still requires memory to be allocated (see What exactly is null in Java memory).

Community
  • 1
  • 1
M A
  • 71,713
  • 13
  • 134
  • 174
0

In your server side if you create an object that contains the integer A, then no matter whether A is initialize or not initialize memory is allocated -

int A; //only declaration of int variable, by default it set to 0
int B = 5; //initialization and declaration, now B is set to 5  

In both case A and B are instance variable that is declared outside of any method of the class but inside of the class. And for method local variable you have to initialize them explicitly.

Razib
  • 10,965
  • 11
  • 53
  • 80