Is memory allocated for unused fields in Java?

I am wondering how memory is allocated in Java.

I have class Duckwith two instance variables int sizeand String name. These variables are initialized. Does the memory save for these variables on the heap at runtime if I do not instantiate this class?

Thanks Gene

+3
source share
2 answers

Several possible scenarios:

  • If you are not using a class, then the class itself is not loaded into the class.

  • If you use a class, but not instantiate, then the instance variables do not take up memory, since there is no instance to start with.

  • , , , , .

+7

Duck , , .

+6

All Articles