Java Static Keyword Intro


Java static 关键字

1、内存图

静态 static 的内存:

2、静态代码块

/**
 * 静态代码块
 * 
 * 特点:当第一次调用到本类时,静态代码块执行唯一的一次
 * 静态内容总是优先于非静态 
 * 
 * 静态代码块的典型用途:
 *  用来一次性地对静态成员变量进行赋值
 */
public class Person {
    
    static {
        System.out.println("静态代码块执行!");
    }
    
    public Person() {
        System.out.println("构造方法执行!");
    }

    public static void main(String[] args) {
        new Person();
    }
}

Author: NaiveKyo
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source NaiveKyo !
  TOC