Java静态代码块中写这个监听方法时的运行原理是什么?

最近再学习过程中遇到了一个问题,看的视频中老师再静态代码块中写了关于hibernate中的SessionFactory关闭的方法,感觉不是很理解,静态代码块中的东西不是在类加载的时候都运行完了嘛。代码如下:

package com.gyf.hibernate.uitls;

import org.dom4j.rule.Rule;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {

    private static SessionFactory factory;
    static{
        //1.获取核心 配置文件对象
        Configuration cfg = new Configuration().configure();

        //2.创建会话工厂
        factory = cfg.buildSessionFactory();

        //监听程序关闭
        Runtime.getRuntime().addShutdownHook(new Thread(){
            @Override
            public void run() {
                System.out.println("程序关闭...");
                //关闭会话工厂
                factory.close();
            }
        });

    }

    public static Session openSession(){
        return factory.openSession();
    }

    public static Session getCurrentSession(){
        return factory.getCurrentSession();
    }

}

注意, factory.close();这个不是在静态代码块中调用的,而是在静态代码块中以匿名实现接口的方式定义的。
作为参数传给了addShutdownHook
因此当关闭的时候才会被触发调用。