简体   繁体   English

为什么我们被限制在java内部类中声明静态成员变量?

[英]Why we are restricted to declare static member variable in inner Class in java?

Consider the below example Why we are restricted to declare static member variable in inner Class when there isn't any restriction on inheriting static variables in Inner classes ?考虑下面的例子为什么我们被限制在内部类中声明静态成员变量,而在内部类中继承静态变量没有任何限制

public class Outer {

    public class Inner {

        public static String notAllowed;
        /* Above line give following compilation error 
         The field notAllowed cannot be declared static in a non-static inner type, unless initialized with a constant expression
         */

    }

}

But now if my inner class extends other class which contains static variable than this works fine.但是现在如果我的内部类扩展了包含静态变量的其他类,那么这可以正常工作。 Consider below code:考虑下面的代码:

public class Outer {

    public class Inner extends InnerBase {
        /* Since it extends InnerBase so we can access Outer.Inner.allowed */ 
        public Inner(){
             Outer.Inner.allowed = null; // Valid statement
        }
    }

}

public class InnerBase {

    public static String allowed;

}

So what is the reason for restricting static variable in inner class as it is achievable through inheritance ?那么在内部类中限制静态变量的原因是什么,因为它可以通过继承实现 Am I missing something very basic?我错过了一些非常基本的东西吗?

From oracle website:来自甲骨文网站:

  1. As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields.与实例方法和变量一样,内部类与其封闭类的实例相关联,并且可以直接访问该对象的方法和字段。
  1. Because an inner class is associated with an instance, it cannot define any static members itself.因为内部类与实例相关联,所以它本身不能定义任何静态成员。

I understand it this way:我是这样理解的:

If inner class have their own static field,and static field have to initialize before class instantiate;如果内部类有自己的静态字段,并且静态字段必须在类实例化之前进行初始化;

But a innner class only exist with an instance of outterclass ,so it can not initialize its static member before instantiate,then in Contradiction.但是内部类只与外部类的实例一起存在,因此它不能在实例化之前初始化其静态成员,然后在矛盾中。

Because in order to access the static field, you will need an instance of the Outer class, from which you will have to create an instance of the non-static Inner class.因为为了访问静态字段,您将需要一个Outer类的实例,您必须从中创建一个非静态Inner类的实例。

static fields are not supposed to be bound to instances and therefore you receive a compilation error. static字段不应绑定到实例,因此您会收到编译错误。

The JLS 8.1.3 specifies: JLS 8.1.3规定:

Inner classes may not declare static initializers or member interfaces, or a compile-time error occurs.内部类可能不会声明静态初始值设定项或成员接口,否则会发生编译时错误。

Inner classes may not declare static members, unless they are constant variables, or a compile-time error occurs.内部类不能声明静态成员,除非它们是常量变量,否则会发生编译时错误。

It is very much possible that the purpose of declaring a static variable opposes the purpose of declaring ANY variable in an inner class.很可能声明静态变量的目的与在内部类中声明任何变量的目的相反。 Static variables are meant to be used statically - in any other static methods and classes, while inner classes imply that those classes serve their outer classes ONLY.静态变量旨在静态使用 - 在任何其他静态方法和类中,而内部类意味着这些类仅为其外部类服务。

I guess the Java creators just wanted it this way.我猜 Java 的创造者只是想要这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们可以将JPA静态元模型类的成员变量声明为final吗? - Can we declare member variable of JPA static metamodel class as a final? 为什么我们要声明 ViewHolder 类是静态的? - Why should we declare the ViewHolder class static? 在Java中声明类和接口的Member变量 - Declare a Member variable of both a class and interface in Java 我可以在Java中的静态成员函数中声明一个静态变量吗? - Can i declare a static variable inside static member function in Java? 为什么我们不能在(非静态)内部类(Java 16 之前)中使用静态方法? - Why can't we have static method in a (non-static) inner class (pre-Java 16)? 为什么ArrayList的非静态内部类SubList有一个成员变量“parent”? - Why ArrayList's non-static inner class SubList has a member variable “parent”? 为什么不能在 Java 中将类声明为静态类? - Why are you not able to declare a class as static in Java? 为什么我们在 selenium 中将 URL 变量声明为私有静态字符串? - Why do we declare URL variable as private static String in selenium? 为什么我们必须将方法声明为java中递归调用的静态方法? - Why do we have to declare method as static for recursive call in java? 为什么在定义类之前要声明嵌套类的变量 - Why can we declare a variable of a nested class before defining the class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM