有的时候,java的初始化会对我的工作照成很大影响,所以简单介绍一下,

首先介绍简单的变量的初始化:在类的内部,变量定义的先后顺序决定了初始化的顺序,即使变量定义散布于方法定义之间,它也会先于构造器和方法初始化。

public class Test{
    public static void main(String[] args){
        Test2 test2 = new Test2();
        test2.f();
    }
}
class Test1{
    public Test1(int i){
        System.out.println("this is Test1"+i);
    }
}
class Test2{

Test1 test1;
    Test1 test11 = new Test1(1);
    public Test2(){
        System.out.println("this is Test2");
        System.out.println("here is test1 "+test1);
        test1 = new Test1(1111);
        test13 = new Test1(33);
    }
    Test1 test12 = new Test1(2);
    public void f(){
        System.out.println("this is f()");
    }
    Test1 test13 = new Test1(3);
}

输出结果为:

this is Test11
this is Test12
this is Test13
this is Test2
here is test1 null
this is Test11111
this is Test133
this is f()

由此可以看出初始化的顺序为:在类的内部,变量定义的先后顺序决定了初始化的顺序,即使变量定义散布于方法定义之间,它也会先于构造器和方法初始化。

即使在构造器或者定义的时候对变量进行了初始化但是这也没办法阻止系统对它进行的默认的初始化;

如果有静态的变量,那么static的变量会优先进行初始化,然后再对普通变量初始化,static变量只初始化一次,当第一次用到这个类的时候进行初始化

最新文章

  1. Atitit Immutability 和final的优点
  2. hdu 3001(状压dp, 3进制)
  3. Flink 剖析
  4. 一个简单的webservice调用
  5. Java 的简单了解
  6. MVC模式下如何对多选框数据进行增删改查
  7. asp.net中的绝对路径和相对路径
  8. wl18xx module crash with "wlcore: ERROR ELP wakeup timeout!"
  9. Testin云測试破7000万次:崩溃成90%手游应用质量难题
  10. 开发中使用mongoTemplate进行Aggregation聚合查询
  11. 关于kali linux 2.0的vmware tools的安装问题
  12. I2C(四)linux3.4(写代码)
  13. 极客时间-左耳听风-程序员攻略-Linux系统、内存和网络
  14. 拉勾网爬取全国python职位并数据分析薪资,工作经验,学历等信息
  15. python ----django---打包重用
  16. linux下umask的使用讲解
  17. bootstrap中模态框的使用
  18. 主动触发input框的失去焦点事件,阻止输入法跳出
  19. libiconv库的安装和使用
  20. schema中字段类型的定义

热门文章

  1. C 中读取键盘码
  2. 在Windows系统搭建.NET Core环境并创建运行ASP.NET网站
  3. Javascript快速入门(下篇)
  4. file:///Users/xmg/Desktop/xiangmu~Bsbdejie/BaisibudejieTheSecondtime/BaisibudejieTheSecond/BaisibudejieTheSecond/AppDelegate.m: warning: Missing file: /Users/xmg/Desktop/xiangmu~Bsbdejie/BaisibudejieT
  5. SSIS package 更新 variable
  6. Script Component 引用package variable
  7. SQL Server Window Function 窗体函数读书笔记二 - A Detailed Look at Window Functions
  8. java中final注意的问题
  9. 国内第一部C#.Net调用Matlab混合编程视频教程
  10. 基于Metronic的Bootstrap开发框架经验总结(3)--下拉列表Select2插件的使用