zoukankan      html  css  js  c++  java
  • java数据域初始化

    1、在声明中赋值

    /**
     * Created by N3verL4nd on 2016/11/19.
     */
    
    class Test{
        private String str = "Hello World";
        public void show(){
            System.out.println(str);
        }
    }
    
    public class HelloWorld {
        public static void main(String[] args) {
            Test t = new Test();
            t.show();
        }
    }

    2、在构造器中设置值

    /**
     * Created by N3verL4nd on 2016/11/19.
     */
    
    class Test{
        private String str;
        Test(){
            str = new String("Hello World");
        }
        public void show(){
            System.out.println(str);
        }
    }
    
    public class HelloWorld {
        public static void main(String[] args) {
            Test t = new Test();
            t.show();
        }
    }

    3、使用初始化块

    /**
     * Created by N3verL4nd on 2016/11/19.
     */
    
    class Test{
        private String str;
        {
            str = new String("Hello World");
        }
        public void show(){
            System.out.println(str);
        }
    }
    
    public class HelloWorld {
        public static void main(String[] args) {
            Test t = new Test();
            t.show();
        }
    }
    
    只要构造类的对象,初始化块就会被执行。

    /**
     * Created by N3verL4nd on 2016/11/19.
     */
    
    class Test{
        {
            str = new String("111");
        }
        private String str = "222";
        Test(){
            str = new String("333");
        }
        public void show(){
            System.out.println(str);
        }
    }
    
    public class HelloWorld {
        public static void main(String[] args) {
            Test t = new Test();
            t.show();
        }
    }
    
    以上代码,调整初始化顺序,单步调试发现,构造函数最后被执行。

    在声明中赋值与在初始化块中设置值则按照顺序执行。

    /**
     * Created by N3verL4nd on 2016/11/19.
     */
    
    class Test{
        Test(){
            str = new String("333");
        }
    
        {
            str = new String("111");
        }
        private String str = "222";
    
        static {
            System.out.println(555);
        }
    
        public void show(){
            System.out.println(str);
        }
    }
    
    public class HelloWorld {
        public static void main(String[] args) {
            Test t = new Test();
            t.show();
        }
    }
    
    static块在类加载的时候被执行,也就是优先于main函数执行。顺序:进入main函数-->static块-->返回main函数。

    无main函数的Hello World

    public class HelloWorld {
        static {
            System.out.println("Hello World");
            System.exit(0);
        }
        /*public static void main(String[] args) {
            Test t = new Test();
            t.show();
        }*/
    }




    Keep it simple!
    作者:N3verL4nd
    知识共享,欢迎转载。
  • 相关阅读:
    【C#语言规范版本5.0学习】2 词法结构(一、简述)
    【C#语言规范版本5.0学习】1.11 特性
    TP5.1 实现超时未支付订单自动关闭
    tp5.1使用队列
    开启队列时,命令行输入php think queue:listen出现乱码
    mysql 查询分组后的总条数
    处理mysql先排序在分组
    mysql 5.7 sql_mode设置 坑
    Windows 版 SourceTree 免登录跳过初始设置的方法和下载地址
    thinkphp5.1-jwt的安装与使用
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/6616297.html
Copyright © 2011-2022 走看看