码迷,mamicode.com
首页 > 编程语言 > 详细

Java之静态代码块

时间:2018-02-12 13:45:50      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:als   oid   const   this   extends   lock   highlight   blog   stat   

有一些情况下,有些代码需要在项目启动的时候就执行,则需要使用静态代码块,这种代码是主动执行的。
Java中的静态代码块是在虚拟机加载类的时候,就执行的,而且只执行一次。如果static代码块有多个,JVM将按照它们在类中出现的先后顺序依次执行它们,每个代码块只会被执行一次。

代码的执行顺序:
  1. 主调类的静态代码块
  2. 对象父类的静态代码块
  3. 对象的静态代码块
  4. 对象父类的非静态代码块
  5. 对象父类的构造函数
  6. 对象的非静态代码块
  7. 对象的构造函数

//静态代码块
static{
...;
}

//静态代码块
{
...;
}

代码举例:
  1 package staticblock;
  2 
  3 public class StaticBlockTest {
  4     //主调类的非静态代码块
  5     {
  6         System.out.println("StaticBlockTest not static block");
  7     }
  8 
  9     //主调类的静态代码块
 10     static {
 11         System.out.println("StaticBlockTest static block");
 12     }
 13 
 14     public StaticBlockTest() {
 15         System.out.println("constructor StaticBlockTest");
 16     }
 17 
 18     public static void main(String[] args) {
 19         // 执行静态方法前只会执行静态代码块
 20         Children.say();
 21         System.out.println("----------------------");
 22 
 23         // 全部验证举例
 24         Children children = new Children();
 25         children.getValue();
 26     }
 27 }
 28 
 29 /**
 30  * 父类
 31  */
 32 class Parent {
 33 
 34     private String name;
 35     private int age;
 36 
 37     //父类无参构造方法
 38     public Parent() {
 39         System.out.println("Parent constructor method");
 40         {
 41             System.out.println("Parent constructor method--> not static block");
 42         }
 43     }
 44 
 45     //父类的非静态代码块
 46     {
 47         System.out.println("Parent not static block");
 48         name = "zhangsan";
 49         age = 50;
 50     }
 51 
 52     //父类静态代码块
 53     static {
 54         System.out.println("Parent static block");
 55     }
 56 
 57     //父类有参构造方法
 58     public Parent(String name, int age) {
 59         System.out.println("Parent constructor method");
 60         this.name = "lishi";
 61         this.age = 51;
 62     }
 63 
 64     public String getName() {
 65         return name;
 66     }
 67 
 68     public void setName(String name) {
 69         this.name = name;
 70     }
 71 
 72     public int getAge() {
 73         return age;
 74     }
 75 
 76     public void setAge(int age) {
 77         this.age = age;
 78     }
 79 }
 80 
 81 /**
 82  * 子类
 83  */
 84 class Children extends Parent {
 85     //子类静态代码块
 86     static {
 87         System.out.println("Children static block");
 88     }
 89 
 90     //子类非静态代码块
 91     {
 92         System.out.println("Children not static block");
 93     }
 94 
 95     //子类无参构造方法
 96     public Children() {
 97         System.out.println("Children constructor method");
 98     }
 99 
100 
101     public void getValue() {
102         //this.setName("lisi");
103         //this.setAge(51);
104         System.out.println(this.getName() + " " + this.getAge());
105     }
106 
107     public static void say() {
108         System.out.println("子类静态方法执行!");
109     }
110 }

 

执行结果:

StaticBlockTest static block
Parent static block
Children static block
子类静态方法执行!
----------------------
Parent not static block
Parent constructor method
Parent constructor method--> not static block
Children not static block
Children constructor method
zhangsan 50

  

 详细:http://uule.iteye.com/blog/1558891




Java之静态代码块

标签:als   oid   const   this   extends   lock   highlight   blog   stat   

原文地址:https://www.cnblogs.com/gongxr/p/8444112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!