JAVA代码块的顺序问题1

时间:2014-01-16 08:35:34   收藏:0   阅读:401
bubuko.com,布布扣
 1 public class Day052
 2 {
 3 
 4     public static void main(String[] args)
 5     {
 6         Zi z = new Zi();
 7         z.print();
 8     }
 9 
10 }
11 
12 class Zi
13 {
14     private int i = setI("setI", 1);
15     private static int j = setJS("setJS", 2);
16 
17     int setI(String s, int i)
18     {
19         System.out.println(s + "  " + "给成员变量赋值 " + i);
20         return i;
21     }
22 
23     static int setJS(String s, int j)
24     {
25         System.out.println(s + "  " + "给静态变量赋值 " + j);
26         return j;
27     }
28 
29     {
30         System.out.println("代码块");
31     }
32     static
33     {
34         System.out.println("static代码块");
35     }
36 
37     Zi()
38     {
39         System.out.println("Zi构造方法");
40     }
41 
42     void print()
43     {
44         System.out.println("成员方法" + " " + i + " " + j);
45     }
46 }
47 // setJS 给静态变量赋值 2
48 // static代码块
49 // setI 给成员变量赋值 1
50 // 代码块
51 // Zi构造方法
52 // 成员方法 1 2
bubuko.com,布布扣

从上面的输出可以看出,使用直接父类为Object的类创建对象过程是:

a、加载类

  1、为静态属性分配内存并赋值

  2、执行静态代码段

b、创建对象

  1、为实例属性分配内存并赋值

  2、执行非静态代码段

  3、执行构造器

原文:http://www.cnblogs.com/linson0116/p/3517496.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!