1.没有对象的构造

public class Test {    

	public static int k = 0;
public static int n = 99; public static int i = print("i");
static {
print("静态块");
} {
print("构造块");
}
public int j = print("j"); public Test(String str) {
System.out.println((++k) + ":" + str + " i=" + i + " n=" + n);
++i;
++n;
} public static int print(String str) {
System.out.println((++k) + ":" + str + " i=" + i +" n=" + n);
++n;
return ++i;
} public static void main(String[] strings) { } }

在载入类的时候构造静态变量和静态代码块。

顺序依照定义的顺序。

结果为:

1:i i=0 n=99

2:静态块 i=1 n=100

2.main函数中构造对象

public class Test {    

	public static int k = 0;
public static int n = 99; public static int i = print("i");
static {
print("静态块");
} {
print("构造块");
}
public int j = print("j"); public Test(String str) {
System.out.println((++k) + ":" + str + " i=" + i + " n=" + n);
++i;
++n;
} public static int print(String str) {
System.out.println((++k) + ":" + str + " i=" + i +" n=" + n);
++n;
return ++i;
} public static void main(String[] strings) {
Test t = new Test("init");
} }

前两条同上。

在构造对象时,先构造对象里的非static变量,再调用构造函数。

非static类的变量按定义的顺序构造。

结果为:

1:i i=0 n=99

2:静态块 i=1 n=100

3:构造块 i=2 n=101

4:j i=3 n=102

5:init i=4 n=103

3.类中含static对象

public class Test {    

	public static int k = 0;
public static int n = 99;
public static Test t1 = new Test("t1"); public static int i = print("i");
static {
print("静态块");
} {
print("构造块");
}
public int j = print("j"); public static Test t2 = new Test("t2"); public Test(String str) {
System.out.println((++k) + ":" + str + " i=" + i + " n=" + n);
++i;
++n;
} public static int print(String str) {
System.out.println((++k) + ":" + str + " i=" + i +" n=" + n);
++n;
return ++i;
} public static void main(String[] strings) {
Test t = new Test("init");
} }

static类型变量、对象、代码块按定义的顺序构造。

构造static对象时候。由于是构造对象所以同2所述,先构造非static变量,在调用构造函数。

结果:

1:构造块 i=0 n=99

2:j i=1 n=100

3:t1 i=2 n=101

4:i i=3 n=102

5:静态块 i=4 n=103

6:构造块 i=5 n=104

7:j i=6 n=105

8:t2 i=7 n=106

9:构造块 i=8 n=107

10:j i=9 n=108

11:init i=10 n=109

4.static变量初始化

public class Test {    

	public static int k = 0;     

	public static Test t1 = new Test("t1");       

	public static int i = print("i");
public static int n = 99;
static {
print("静态块");
} {
print("构造块");
}
public int j = print("j"); public static Test t2 = new Test("t2"); public Test(String str) {
System.out.println((++k) + ":" + str + " i=" + i + " n=" + n);
++i;
++n;
} public static int print(String str) {
System.out.println((++k) + ":" + str + " i=" + i +" n=" + n);
++n;
return ++i;
} public static void main(String[] strings) {
Test t = new Test("init");
} }

先默认初始化再进行赋值。

结果:

1:构造块 i=0 n=0

2:j i=1 n=1

3:t1 i=2 n=2

4:i i=3 n=3

5:静态块 i=4 n=99

6:积木 i=5 n=100

7:j i=6 n=101

8:t2 i=7 n=102

9:积木 i=8 n=103

10:j i=9 n=104

11:init i=10 n=105

Java一流的施工顺序的更多相关文章

  1. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  2. 打造Linux三流娱乐环境,二流办公环境,一流Java开发环境

    写这篇文章的目的首先是为让自己以后再装linux环境时候,不用再通宵google+百度,做个备忘录,其次,给新入Linux环境的同学分享一点个人经验,再高尚点的动机也算是想做为开源技术的传播布道者.我 ...

  3. Java学习-序列化

    参考资料: http://www.2cto.com/kf/201405/305380.html http://www.cnblogs.com/xdp-gacl/p/3777987.html   序列化 ...

  4. 推荐一篇关于java 学习的文章,感觉写的很不错

    ---恢复内容开始---    很多网友问我学习Java有没有什么捷径,我说"无他,唯手熟尔".但是我却很愿意将自己学习的一些经验写出来,以便后来者少走弯路,帮助别人是最大的快乐嘛 ...

  5. java.io.Serializable 序列化接口

    什么是序列化.反序列化? Serialization(序列化)是一种将对象以一连串的字节描述的过程: 反序列化deserialization是一种将这些字节重建成一个对象的过程. 序列化通俗一点说就是 ...

  6. 2016-2017-2 《Java程序设计》预备作业2总结

    2016-2017-2 <Java程序设计>预备作业2总结 古希腊学者普罗塔戈说过:「头脑不是一个要被填满的容器,而是一束需要被点燃的火把.」 在对计算机系的学生情况的调查中,我说: 最近 ...

  7. 2016-2017-2 《Java程序设计》预备作业1 总结

    2016-2017-2 <Java程序设计>预备作业1 总结 预备作业01:你期望的师生关系是什么见https://edu.cnblogs.com/campus/besti/2016-20 ...

  8. 招聘前端、Java后端开发、测试、Mysql DBA

    公司介绍: http://www.lagou.com/gongsi/43095.html http://www.yamichu.com 简历发到: zhuye@yamichu.com 招聘职位: JA ...

  9. Java中的Serializable接口transient关键字,及字节、字符、对象IO

    1.什么是序列化和反序列化Serialization是一种将对象转为为字节流的过程:deserialization是将字节流恢复为对象的过程. 2.什么情况下需要序列化a)当你想把的内存中的对象保存到 ...

随机推荐

  1. zoj1940(三维广搜)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=940 分析:三维其实就是六个方向地搜索,思维清晰且细心点,很快就AC了 ...

  2. Vagrant - 百度百科

    http://wapbaike.baidu.com/view/9201587.htm?ssid=0&from=844b&uid=3151E6C0905477A13653132D762B ...

  3. HDSF主要节点解说(二)工作原理

    HDFS(Hadoop Distributed File System )Hadoop分布式文件系统. 是依据google发表的论文翻版的.论文为GFS(Google File System)Goog ...

  4. xcode Workspaces

    A workspace is an Xcode document that groups projects and other documents so you can work on them to ...

  5. c#事件委托

    转载地址:http://www.cnblogs.com/wudiwushen/archive/2010/04/20/1703368.html    从序言中,大家应该对委托和事件的重要性有点了解了吧, ...

  6. 处理json中影响解析的多余引號

    在xml中,敏感字符是尖括号,在json中,敏感字符是引號,上文中我们介绍了怎样处理xml中的敏感字符.本文说说怎样处理json中的敏感字符. 思路与上文同样,不再赘述.直接上代码: json–> ...

  7. 怎样从host之外连接到docker container

    启动docker的时候的指令使用 sudo docker -H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock -d & 这样就能使dock ...

  8. 两个div在同一行,两个div不换行

    方法一: <div style="display:inline"> <div id="div1" style="float:left ...

  9. c语言移位操作

    应该先看看C语言是指所有的位二进制算术位计算.即使输入的是十进制的数,在存储器存储为二进制形式. “<<”使用方法: 的格式是:a<<m,a和m式,要求m>=0. 功能: ...

  10. SQL Server :理解数据记录结构

    原文:SQL Server :理解数据记录结构 在SQL Server :理解数据页结构我们提到每条记录都有7 bytes的系统行开销,那这个7 bytes行开销到底是一个什么样的结构,我们一起来看下 ...