1、从头认识java-4.7构造器初始化(2)这一章节我们接着上一章节的内容。(3)静态数据的初始化a.静态数据只会执行一次[java] viewplaincopy1.package com.ray.testobject; 2. 3.public class Test { 4. private static Book book1 = new Book(1); 5. private static Book book2 = new Book(2); 6. 7. public Test() { 8. Sys
2、tem.out.println("create test"); 9. book1.say(); 10. book2.say(); 11. Book book3 = new Book(3); 12. book3.say(); 13. } 14. 15. public static void main(String[] args) { 16. new Test(); 17. new Test(); 18. } 19.} 2
3、0. 21.class Book { 22. private int id; 23. 24. public Book(int id) { 25. this.id = id; 26. System.out.println("create book:" + id); 27. } 28. 29. public void say() { 30. System.out.println("hi, my name is book-" + id); 31. } 3