java(12)------static关键字

来源:互联网 发布:淘宝亲宝贝什么意思 编辑:程序博客网 时间:2024/06/05 17:56

static在类加载时执行,同时只执行一次;

package com.lanhuigu.java;public class StaticInitialization {public static void main(String[] args) {System.out.println("Creating new School() in main");new School();System.out.println("Creating new School() in main");teacher.f2(1);school.f3(1);}static Teacher teacher = new Teacher();static School school = new School();}class Student {Student(int marker) {System.out.println("Student(" + marker +")");}void f1(int marker) {System.out.println("f1("+ marker +")");}}class Teacher {static Student s1 = new Student(1);Teacher() {System.out.println("Teacher()");s2.f1(1);}void f2(int marker) {System.out.println("f2(" + marker + ")");}static Student s2 = new Student(2);}class School {Student s3 = new Student(3);static Student s4 = new Student(4);School() {System.out.println("School()");s4.f1(2);}void f3(int marker) {System.out.println("f3("+ marker +")");}static Student s5 = new Student(5);}
程序控制台运行结果:

Student(1)
Student(2)
Teacher()
f1(1)
Student(4)
Student(5)
Student(3)
School()
f1(2)
Creating new School() in main
Student(3)
School()
f1(2)
Creating new School() in main
f2(1)
f3(1)

结合程序分析static执行顺序和次数:

在运行程序时,控制台没有打印Creating new School() in main,说明并没有执行main中的

System.out.println("Creating new School() in main");

然而控制台中打印了Student(1),这个是从StaticInitialization类下的static Teacher teacher = new Teacher();

执行而来,StaticInitialization类中先执行static Teacher teacher = new Teacher();静态代码按顺序先执行

在Teacher类中代码如下:

class Teacher {
    static Student s1 = new Student(1);
    
    Teacher() {
        System.out.println("Teacher()");
        s2.f1(1);
    }
    void f2(int marker) {
        System.out.println("f2(" + marker + ")");
    }
    
    static Student s2 = new Student(2);
}

在Teacher类中,我们可以看到在类开始和结束位置都分别有两个静态代码,

按顺序先执行 static Student s1 = new Student(1);调用Student类中的构造方法:

Student(int marker) {
        System.out.println("Student(" + marker +")");
 }

在控制台打印Student(1),然后执行Teacher类中的第二个静态代码 static Student s2 = new Student(2);

同样调用Student类中的构造器打印出Student(1),最后再执行Teacher类中的构造器:

Teacher() {
        System.out.println("Teacher()");
        s2.f1(1);
    }

打印出Teacher(),然后通过s2调用Student类中的f1方法,打印出f1(1),这样我们的StaticInitialization

中第一个静态代码static Teacher teacher = new Teacher();执行完毕。

然后再按顺序执行StaticInitialization类中的第二个静态代码static School school = new School();

在School类中执行顺序如下:

按照先执行第一个静态代码static Student s4 = new Student(4);

第二个静态代码static Student s5 = new Student(5);

再执行Student s3 = new Student(3);

最后执行构造器:

School() {
        System.out.println("School()");
        s4.f1(2);
 }

的顺序执行代码,打印出对应的内容。

在StaticInitialization类中执行完静态代码

static Teacher teacher = new Teacher();
static School school = new School();

然后再执行main方法:

System.out.println("Creating new School() in main");

new School();

......

对于main中的new School();在School类中执行时不会再执行其中的静态代码,

也就是说不会再执行static Student s1 = new Student(1);static Student s2 = new Student(2);

因为静态代码只执行一次,在开始的时候已经执行了,这个地方再School类中不会再执行。

依次执行main中的方法,打印出相关信息,参考控制台结果分析程序。


注: static可以用于静态代码块,其类加载时先执行,只执行一次的性质不变。

0 0
原创粉丝点击