首页 > 代码库 > static关键字修饰的属性及调用顺序

static关键字修饰的属性及调用顺序

 1 package Day11;
 2 
 3 public class StaticInitialization {
 4     static Table table=new Table();  //Bow11 Bow12 Table() f1(1) 
 5     static Cupboard cupboard=new Cupboard();//Bow14 Bow15 Bow13 Cupboard() f1(2)  
 6     public static void main(String[] args) {
 7         System.out.println("Creating new Cupboad() in main");//Creating new Cupboad() in main
 8         new Cupboard();//Bow13 Cupboard() f1(2)
 9         System.out.println("Creating new Cupboad() in main");//Creating new Cupboad() in main
10         new Cupboard();//Bow13 Cupboard() f1(2)
11         table.f2(1);//f2(1)
12         cupboard.f3(1);//f3(1)
13     }
14 }
15 class Cupboard{
16     Bow1 bowl3=new Bow1(3);
17     static Bow1 bow4=new Bow1(4);
18     public Cupboard() {
19         System.out.println("Cupboard()");
20         bow4.f1(2);
21     }
22     void f3(int marked){
23         System.out.println("f3("+marked+")");
24     }
25     static Bow1 bow5=new Bow1(5);
26 }
27 class Bow1{
28     public Bow1(int marked) {
29         System.out.println("Bow1("+marked+")");
30     }
31     public void f1(int marked){
32         System.out.println("f1("+marked+")");
33     }
34 }
35 class Table{
36     static Bow1 bow1=new Bow1(1);
37     static Bow1 bow2=new Bow1(2);
38     public Table() {
39         System.out.println("Table()");
40         bow2.f1(1);
41     }
42     void f2(int marked){
43         System.out.println("f2("+marked+")");
44     }
45 }

 

 

 

 

 

 

 

 

 

 

 1 package Day11;
 2 
 3 import java.util.Arrays;
 4 
 5 public class StaticDemo {
 6     static People p=new People(); 
 7     public static void main(String[] args) {
 8         System.out.println("=======================");
 9     }
10 }
11 class People{
12     Student stu1=new Student(1);
13     public People(){
14         System.out.println("***************");
15     }
16 }
17 class Student{
18     static People p=new People(); 
19     public Student(int a){
20         System.out.println("+++++++++++++++++"+a);
21     }
22 }

打印结果:

+++++++++++++++++1
***************
+++++++++++++++++1
***************
=======================

static关键字修饰的属性及调用顺序