Is there a way to stop static variable from being loaded into memory?
Thanks,
You can stop a static variable loading by not loading the class, or having the static initializer throw an exception/error. Either way the class is unusable.
If you have a compiled class and you want to change the way it loads, you can decompile it and change it manually or use byte code manipulation at runtime. The first option is likely to be simpler.
If you do not need them anyways, why don't you remove them?
If you do need them, but only at a much later point, and they are so large that optimization really makes sense, use lazy initialization. For this, you can for instance use Lombok's annotation @Getter(lazy=true)
(which implements the double-check idiom, see Effective Java Item 71).
There is a strict order of initialization so it cannot be postponed for later.
I do not know if ripping the variables from bytecode can be done and what is the required effort.
精彩评论