What is the difference between a static and a non-static initialization code block

后端 未结 8 1953
萌比男神i
萌比男神i 2020-11-21 23:32

My question is about one particular usage of static keyword. It is possible to use static keyword to cover a code block within a class which does not belong to

8条回答
  •  情深已故
    2020-11-22 00:11

    The static block is a "static initializer".

    It's automatically invoked when the class is loaded, and there's no other way to invoke it (not even via Reflection).

    I've personally only ever used it when writing JNI code:

    class JNIGlue {
        static {
            System.loadLibrary("foo");
        }
    }
    

提交回复
热议问题