static 深入理解

import java.io.PrintStream;
import java.util.Date;

class Bow{
    public Bow(int i) {
        // TODO Auto-generated constructor stub
        System.out.println("bow "+i);
    }
    
    void print(int i){
        System.out.println("print "+i);
    }
}

class Table{
    static Bow bow1=new Bow(1);
    public Table() {
        // TODO Auto-generated constructor stub
        System.out.println("table++444");
        bow1.print(1);
    }
    
    
    void  print2(int i){
        System.out.println("print2 "+i);
    }
    
    static Bow bows2 = new Bow(2);  //bow2
}

class Cupborard{
    Bow bow3=new Bow(3);
    static Bow bow4=new Bow(4);
    
    public Cupborard() {
        // TODO Auto-generated constructor stub
        System.out.println("Cupborard");
        bow4.print(2);
    }
    
    void print3(int i){
        System.out.println("print3"+i);
    }
    
    static Bow bow5=new Bow(5);
}


public class baby {
    static Table table= new Table();
    static Cupborard cupboard=new Cupborard();
    public static void main(String[] args) throws Throwable {
        System.out.println();
        System.out.println("main");
        new Cupborard();
        System.out.println("ma");
        new Cupborard();
        table.print2(1);
        cupboard.print3(3);
    }

}


//////////////////////////




think of java
加深对static  jvm 初始化类


如上例子,
jvm 对生一个类,首先是找出public class具有static 的类(Table Cupborad),先初始化,然后作为全局变量使用类也要保存,

当初始化完后。在执行main函数


从这里开始  

public class baby {

 public classs 初始化{
        static Table table= new Table();
        static Cupborard cupboard=new Cupborard();
        }


bow 1
bow 2
table++444
print 1
bow 4
bow 5
bow 3
Cupborard
print 2

             执行main
main

bow 3
Cupborard
print 2
ma

bow 3
Cupborard
print 2



print2 1
print33


------------------------

import java.io.PrintStream;
import java.util.Date;

class Bow{
    long i=99996;
    
     {
        System.out.println("ko");    
        System.out.println(this.i);
    }
     public Bow() {
        // TODO Auto-generated constructor stub
         System.out.println("77");
    }
    

    
}


public class baby {
    
    public static void main(String[] args) throws Throwable {
         Bow bow1 = new Bow();
         Bow bow2 = new Bow();
    }

}

-------------------------

static 代码块    代码块  初始话成员变量 在执行构造 申请空间


多花时间作比比较难的事,你就不好那么被取代



















































你可能感兴趣的:(Think,of,java)