Cannot declare Public static final String s = new String(“123”) inside an inner class

后端 未结 4 1442
清酒与你
清酒与你 2021-01-05 15:26

I tried to declare a class as shown below

class Outer{
    private final class Inner{
      public static final String s1 = new String(\"123\");
      public         


        
4条回答
  •  生来不讨喜
    2021-01-05 15:59

    Inner classes were designed to work in the context of the outer class, I think static variables would break this rule.

    8.1.2 Inner Classes and Enclosing Instances

    An inner class is a nested class that is not explicitly or implicitly declared static. Inner classes may not declare static initializers (§8.7) or member interfaces. Inner classes may not declare static members, unless they are compile-time constant fields (§15.28).

提交回复
热议问题