首页 > 代码库 > 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关键字修饰的属性及调用顺序
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。