• The Key to final data


    // FinalData.java
    
    package com.hp.ci.mgmt.perm.hal.localization;
    
    import java.util.Random;
    
    class Value
    {
        int i;
    
        public Value(final int i)
        {
            this.i = i;
        }
    
    }
    
    /**
     *
     */
    public class FinalData
    {
        private static Random rand = new Random(47);
    
        private String id;
    
        public FinalData(final String id)
        {
            this.id = id;
        }
    
        // Can be compile-time contants
        private final int valueOne = 9;
        private static final int VALUE_TWO = 99;
        //Typical public constant
        public static final int VALUE_THREE = 39;
        //Cannot be compile-time constants
        private final int i4 = rand.nextInt(20);
        static final int INT_5 = rand.nextInt(20);
    
        private Value v1 = new Value(11);
        private final Value v2 = new Value(22);
        private static final Value VAL_3 = new Value(33);
    
        //Arrays:
        private final int[] a = {
                1, 2, 3, 4, 5, 6
        };
    
        @Override
        public String toString()
        {
            return id + ": " + "i4 = " + i4 + ", INT_5 = " + INT_5;
        }
    
        /**
         * @param args
         */
        public static void main(final String[] args)
        {
            final FinalData fd1 = new FinalData("fd1");
            //! fd1.valueOne++; // Error: can't change value
            fd1.v2.i++; // Object isn't contant
            fd1.v1 = new Value(9); //Ok -- not final
            for (int i = 0; i < fd1.a.length; i++)
            {
                fd1.a[i]++; //Object isn't constant!
            }
    
            //! fd1.v2 = new Value(0); //Error: Can't
            //! fd1.VAL_3 = new Value(1); // change reference
            //! fd1.a = new int[3];
            System.out.println(fd1 + "
    Creating new FinalData");
            final FinalData fd2 = new FinalData("fd2");
    
            System.out.println(fd1);
            System.out.println(fd2);
        }
    
    }

     -- Copy From Thinking in Java Fourth Edition

  • 相关阅读:
    伯克利推出世界最快的KVS数据库Anna:秒杀Redis和Cassandra
    不要什么都学-打造自己的差异化价值
    gitlab markdown支持页面内跳转
    技术人员怎样提升对业务的理解
    为什么HDFS的副本数通常选择3?
    MySQL++简单使用记录.md
    log4cpp安装使用
    log4cxx安装使用
    epoll使用总结
    工作方法-scrum+番茄工作法
  • 原文地址:https://www.cnblogs.com/title/p/5147547.html
Copyright © 2020-2023  润新知