【问题标题】:What does it mean if a static inner class is also final? [duplicate]如果静态内部类也是最终的,这意味着什么? [复制]
【发布时间】:2015-10-01 17:39:18
【问题描述】:
public class ClassWithInnerClass {

    int a = 10;

    public static void outer(){
        System.out.println("In method of outer class");

    }


        final static class Inner{

            int b = 20;

            public void innermethod(){
                System.out.println("In method of inner class");
                System.out.println("Inner class variable b = "+b);

            }
        }
}

在上面的代码中,我有一个外部类,然后有一个带有非访问说明符“final”的静态嵌套类。这是否使这个类类似于“常量”变量?

【问题讨论】:

标签: java static inner-classes final


【解决方案1】:

不,它与常量变量不同。这意味着你不能创建这个嵌套类的子类(Inner)。这类似于在顶级(即非嵌套)类中使用 final 关键字。

【讨论】:

    【解决方案2】:

    final 在类上意味着它们不能被扩展。这与在顶层定义的类上使用 final 没有什么不同。

    final 在类上final 在变量上相同。 final 根据您使用它的位置而具有不同的含义。一般来说,它描述了一种不变性:

    • 在变量上,这意味着一旦初始化就不能改变它的值。
    • 在方法上,意味着一旦定义,就不能在子类中被覆盖。
    • 在类上,这意味着类本身不能被子类化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      • 2017-03-09
      • 2016-12-18
      • 2015-12-20
      • 1970-01-01
      • 2020-05-31
      • 1970-01-01
      相关资源
      最近更新 更多