经典指数          
原因
1325
浏览数
0
收藏数
 

When is the text “Hi there”displayed?
public class Static Test
{
    static
    {
        System.out.println(“Hi there”);
    }

    public void print()
    {
        System.out.println(“Hello”);
    }

    public static void main(String args[])
    {
        Static Test st1 = new StaticTest();
        st1.print();
        StaticTest st2 = new StaticTest();
        st2.print();
    }
}

  • Never.
  • Each time a new object of type StaticTest is created.
  • Once when the class is loaded into the Java virtual machine.
  • Only when the main() method is executed.

     举报   纠错  
 
切换
1 个答案

这题有个很巧妙的地方,正常是main中调用了new才导致class被加载,这儿把static放到main的类中,程序刚启动变会自动加载main所在的类

 
切换
撰写答案
扫描后移动端查看本题