Search code examples
java.class-file

Java - uninitialized static variable in .class file


In c, uninitialized static variable don't take space in executable file.

What about Java? Does a static variable take space in .class file?

Tip: The question is about disk space, not memory.


Solution

  • All fields have an entry in the classfile giving the name, type, flags (final, public, volatile, static, etc.), and other data.

    If you think about it, it has to be this way. Java isn't C, where the variable is just a location in bss or whatever. You have reflection as well as runtime type checking, so all that information has to be maintained.

    If the field is initialized, there will be additional data in the classfile to do the initialization (either a ConstantValue attribute or bytecode in the method). However, even an uninitialized field has to have the field entry in the classfile.