Static blocks are executed when class is loading even before the public static void main
starts. Is there any possibility to run a certain code just before the program close?
I think you're looking for Runtime.addShutdownHook
. It's not a language level construct in the way that static initializer blocks are, but I think it'll do what you want.
You should be careful with shutdown hooks though - see the documentation for various warnings around them.
See more on this question at Stackoverflow