Search code examples
clojureclojure-java-interop

What means "public static {}" in bytecode generated by clojure compiler?


When I generate a class in clojure (through gen-class), I get the following definition upon inspection via javap:

public class foo.bar extends java.lang.Object implements java.io.Serializable{
    public final java.lang.Object state;
    public static {};
    public foo.bar();
    ...
}

I wonder what the construct public static {} means as I never saw something like this before …

Can someone please enlighten me?


Solution

  • The static section contains code which runs during static class initialization (before any instances of the class are created).

    Think of having namespace-level code with side effects in Clojure -- those side effects take place as soon as anyone requires or uses the namespace, even if they don't actually call any functions. This is a comparable situation.