package demo;

 /*
* 在类 的内部,变量定义的先后顺序决定了初始化的顺序。即使变量定义散布于方法定义之间,
* 它们仍旧会在任何方法(包括构造器)被调用之前得到初始化。
*/
public class Test {
public static void main(String[] args) {
House h = new House();
h.f();
}
} class Window {
Window(int maker) {
System.out.println("Window(" + maker + ")");
}
} class House {
Window w1 = new Window(1); House() {
System.out.println("House()");
w3 = new Window(33);
} Window w2 = new Window(2); void f() {
System.out.println("f()");
} Window w3 = new Window(3);
}
//结果:
//Window(1)
//Window(2)
//Window(3)
//House()
//Window(33)
//f()
 package demo;

 /*
* 先执行static修饰的成员,而且只被执行一次
*/
public class Test1 {
public static void main(String[] args) {
System.out.println("Creating new Cup() in main");
new Cup();
System.out.println("Creating new Cup() in main");
new Cup();
table.f2(1);
cup.f3(1);
} static Table table = new Table();
static Cup cup = new Cup();
} class Bowl {
Bowl(int maker) {
System.out.println("Bowl(" + maker + ")");
} void f1(int maker) {
System.out.println("f1(" + maker + ")");
}
} class Table {
static Bowl bowl1 = new Bowl(1); Table() {
System.out.println("Table()");
bowl1.f1(1);
} void f2(int maker) {
System.out.println("f2(" + maker + ")");
} static Bowl bowl2 = new Bowl(2);
} class Cup {
Bowl bowl3 = new Bowl(3);
static Bowl bowl4 = new Bowl(4); Cup() {
System.out.println("Cup()");
bowl4.f1(2);
} void f3(int maker) {
System.out.println("f3(" + maker + ")");
} static Bowl bowl5 = new Bowl(5);
}
结果:
Bowl(1)
Bowl(2)
Table()
f1(1)
Bowl(4)
Bowl(5)
Bowl(3)
Cup()
f1(2)
Creating new Cup() in main
Bowl(3)
Cup()
f1(2)
Creating new Cup() in main
Bowl(3)
Cup()
f1(2)
f2(1)
f3(1)

构造器初始化(static)的更多相关文章

  1. Chapter5_初始化与清理_构造器初始化

    一.构造器初始化的基本顺序 在使用构造器进行初始化时,最需要注意的是初始化的顺序,这种方法可以给初始化的顺序带来很大的灵活性.看如下的一个例子. class Window{ Window(int ma ...

  2. Chapter5_初始化与清理_用构造器初始化

    接下来进入第五章,java中初始化和清理的问题,这是两个涉及安全的重要命题.初始化的功能主要是为库中的构件(或者说类中的域)初始化一些值,清理的功能主要是清除程序中不再被需要的元素,防止资源过分被垃圾 ...

  3. Java基础语法05-面向对象-封装-包-构造器-初始化

    封装 面向对象三大特性:封装.继承.多态 封装的好处 1.调用者:方便使用/简化使用 2.设计者:安全,可控 隐藏对象内部的复杂性,只对外公开简单的接口.便于外界调用,从而提高系统的可扩展性.可维护性 ...

  4. 使用Objective-C的+(void)initialize初始化static变量

    在<Objective C类方法load和initialize的区别>一文中,我介绍了Objective-C对待+(void)initialize和+(void)load两个方法在编译和执 ...

  5. Spring 类构造器初始化实例

    构造方法类Bean1 package com.hao947.bean; public class Bean1 { public Bean1() { System.out.println("b ...

  6. java初始化笔记

    构造器初始化static块与非static块区别:对象创建时都做块的初始化,非static块仅在实例创建时初始化,static块在类被第一次加载时初始化数组初始化注意点:1.数组定义时最好在定义的同时 ...

  7. C++ static、const和static const 以及它们的初始化

    转自C++ static.const和static const 以及它们的初始化 const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会释放其存储空间. s ...

  8. 【转】forbids in-class initialization of non-const static member不能在类内初始化非const static成员

    转自:forbids in-class initialization of non-const static member不能在类内初始化非const static成员 今天写程序,出现一个新错误,好 ...

  9. Java静态变量的初始化(static块的本质)

    Java静态变量的初始化(static块的本质) 标签: javaclassstring编译器jdk工作 2010-02-06 07:23 33336人阅读 评论(16) 收藏 举报  分类: Jav ...

随机推荐

  1. Python实现C代码统计工具(一)

    目录 Python实现C代码统计工具(一) 声明 一. 问题提出 二. 代码实现 三. 效果验证 四. 后记 Python实现C代码统计工具(一) 标签: Python 代码统计 声明 本文将基于Py ...

  2. android rom开发

    How to Build Android ROMs on Ubuntu 16.04https://www.digitalocean.com/community/tutorials/how-to-bui ...

  3. 使用SpringContextHolder获取bean实例

    public static IConstantFactory me(){ return SpringContextHolder.getBean(beanName:"constantFacto ...

  4. spring框架排错

    1.格式错误. http://www.springframework.org/schema/mvc       http://www.springframework.org/schema/mvc/sp ...

  5. 使用python删除一个文件或文件夹

    使用python删除一个文件或文件夹,需要使用os模块. import osos.remove(path) # path是文件的路径,如果这个路径是一个文件夹,则会抛出OSError的错误,这时需用用 ...

  6. [LintCode] Majority Number 求大多数

    Given an array of integers, the majority number is the number that occurs more than half of the size ...

  7. web -- Navigator.vibrate(); 使设备(有振动硬件)产生有频率的振动

    MDN 文档 此方法需要用户手势. 否则,它返回false. const koa2 = require(`koa2`); const Router = require(`koa-router`); c ...

  8. MPD软件工作坊北京站:技术创新与研发效率带来的前沿思考

    在新技术层出不穷.不断迭代的当下,多数企业都在面临技术能力提升,认知升级等问题.面对技术企业的研发环节,为什么你的效率总是提不上来?都在寻找创新的技术领域,为何别人总能抢占先机?提升自己的研发竞争力, ...

  9. python中OrderedDict的使用

    很多人认为python中的字典是无序的,因为它是按照hash来存储的,但是python中有个模块collections(英文,收集.集合),里面自带了一个子类 OrderedDict,实现了对字典对象 ...

  10. maven工程之pom模板(hadoop、hive、hbase)

    以下配置文件涵盖了hadoop.hive.hbase开发支持库的配置. 仅需针对maven工程pom.xml文件做相应更改就可以自动生成hadoop开发支持库. <properties>  ...