简体   繁体   English

一次可以做多少次<clinit>方法在Java中执行?

[英]How many times can a <clinit> method execute in Java?

说一个方法只为特定类执行一次是否正确?

I have created Test class and referred it as follows我创建了 Test 类并将其引用如下

  • new operator运营商
  • reflection反射

The < clinit > block got referred 1 time only < clinit > 块仅被引用1 次

public class ClinitTest {

    public static void main(String[] args) throws ClassNotFoundException {
        Test b = new Test(); // initialization through NEW operator
        Class<Test> bcp2 =(Class<Test>) Class.forName("Test"); //initialisation through reflection
    }

}

class Test
{
    public static String name="Test";
    public Test()
    {
        System.out.println("In Constructor");
    }

    static
    {
        System.out.println("In Static");
    }

    {
        System.out.println("In Instance");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM