• 问题1--static final关键词


    如下面所述代码,scf输出scf = SelfCounter id,后面id递增,可以理解。而由于scsf被static final修饰了,无论再new多少次输出都不会变,但是为什么输出是0而不是1呢?百思不得解,小白求拍求解答!

    class SelfCounter {
      private static int count;
      private int id = count++;
      public String toString() { return "SelfCounter " + id; }
    }

    class WithFinalFields {
      final SelfCounter scf = new SelfCounter();
      static final SelfCounter scsf = new SelfCounter();
      public String toString() {
        return "scf = " + scf + " scsf = " + scsf;
      }
    }
    public class Final_Static {
      public static void main(String args[]) {
        System.out.println("First object:");
        System.out.println(new WithFinalFields());
        System.out.println("Second object:");
        System.out.println(new WithFinalFields());
      }
    }/* Output: 

    First object:
    scf = SelfCounter 1
    scsf = SelfCounter 0
    Second object:
    scf = SelfCounter 2
    scsf = SelfCounter 0
    *///:~

  • 相关阅读:
    Jedis测试redis
    jedis池的作用
    错误
    Ceph剖析:数据分布之CRUSH算法与一致性Hash
    drools规则引擎初探
    Techniques for HA IT Management
    django_simple_captcha使用笔记
    微服务架构的理论基础
    分布式系统服务的稳定性
    四层、七层负载均衡的区别
  • 原文地址:https://www.cnblogs.com/xiaoxionganna/p/8901891.html
Copyright © 2020-2023  润新知