A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© xiaoya0o0o 中级黑马   /  2015-9-17 21:52  /  679 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

.类的初始化过程:
                加载class文件
                堆中开辟空间
                变量的默认初始化
                变量的显示初始化
                构造代码块初始化
                构造方法初始化
          成员变量-->构造代码块-->构造方法
  1. class Student
  2. {
  3.         static int[] array;

  4.         public Student(){
  5.                 array = new int[10];
  6.                 System.out.println("构造方法");
  7.                
  8.         }
  9.         {
  10.                 System.out.println("构造代码块");
  11.         }
  12.         static{
  13.                 array = new int[10];
  14.                 System.out.println("静态代码块");
  15.         }
  16. }
  17. class Demo
  18. {
  19.         public static void main(String[] args)
  20.         {
  21.                 Student stu = new Student();
  22.                 System.out.println("-----------------------");
  23.                 Student stu1 = new Student();
  24.         }
  25. }
复制代码

4 个回复

倒序浏览
  1. /*
  2.         代码块:

  3.         1.被括在一对大括号之间的一块代码;
  4.         2.代码块的分类:
  5.                 1).局部代码块:
  6.                                 在某个方法中,或者某个代码块中;
  7.                                 执行:顺序执行;
  8.                                 作用:限制变量的声明周期;
  9.                                 main(){
  10.                                         {
  11.                                                 int n = 10;//如果这个非常占用内存
  12.                                                 System.out.println("n = " + n);//模拟使用变量
  13.                                         }
  14.                                         //代码到这里,就释放了n的内存;
  15.                                 }
  16.                 2).构造代码块:
  17.                                 定义在"类体中";
  18.                                 执行:每次实例化这个类的对象时,都会被执行一次;
  19.                                 作用:一般也是用于类的初始化操作,跟"构造方法"的作用一样,区别:构造代码块不能带参数;
  20.                                 注意:如果跟构造方法同时存在,先执行"构造代码块";
  21.                 3).静态代码块:
  22.                                 定义在"类体中";使用static修饰的一个代码块;
  23.                                 执行:第一次使用这个类时,会被执行一次;之后就不再被执行;
  24.                                 作用:用于初始化"静态成员";它可以访问成员变量,但仍然只能访问静态成员变量;
  25.                                 注意:执行顺序:静态代码块-->构造代码块-->构造方法
  26.                           
  27. */
  28. class Student
  29. {
  30.         String name;
  31.         static int i;
  32.         //构造代码块
  33.         {
  34.                 System.out.println("Student的构造代码块2......");
  35.         }
  36.         {
  37.                 System.out.println("Student的构造代码块1......");
  38.         }

  39.         //构造方法
  40.         Student(){
  41.                 System.out.println("Student的无参构造方法!");
  42.         }

  43.         //静态代码块
  44.         static{
  45.                 System.out.println("静态代码块2...... i = " + i++);
  46.         }
  47.         static{
  48.                 System.out.println("静态代码块1...... i = " + i);
  49.         }
  50.        

  51.        
  52. }
  53. class Demo
  54. {
  55.         public static void main(String[] args)
  56.         {
  57.                 Student stu1 = new Student();
  58.                 Student stu2 = new Student();
  59.                 Student stu3 = new Student();
  60.         }

  61.        
  62. }
复制代码
回复 使用道具 举报
真是不错哦
回复 使用道具 举报
.。。。。我明白了
回复 使用道具 举报
这个挺难的,搞不太懂
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马