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.

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.