举例子:

package test_instance;

public class TestClassLoaderTime {  
       
       public TestClassLoaderTime(){  
            System.out.println("构造器执行");  
       }
       
       {  
             System.out.println("静态代码块执行");  
       }  
       static Demo2 demo2 = new Demo2();  
       
       private Demo3 demo3 = new Demo3();  
       
       public static void main(String[] args) {  
              System.out.println("main方法执行");  
              new TestClassLoaderTime();  
              System.out.println("TestClassLoaderTime实例化过");  
        }  
       
    }  


执行结果:

Initialization the Demo2.....
main方法执行
静态代码块执行
Initializaiton the Demo3....
构造器执行
TestClassLoaderTime实例化过

所以是按照如下顺序执行的:

1.Demo2的构造函数执行   (静态属性)
2.main方法执行  
3.静态代码块执行  
4.Demo3的构造函数执行    (非静态属性)
5.构造器执行               
6.TestClassLoaderTime实例化过  

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-25
  • 2021-09-10
  • 2022-12-23
  • 2022-12-23
  • 2022-01-11
  • 2021-10-15
猜你喜欢
  • 2022-12-23
  • 2022-01-29
  • 2021-07-17
  • 2022-01-21
  • 2021-08-13
  • 2021-04-12
  • 2021-06-17
相关资源
相似解决方案