为什么非静态内部类不能有静态成员(字段和方法)? [英] Why a non-static inner-class cannot have static members (fields and methods)?

查看:34
本文介绍了为什么非静态内部类不能有静态成员(字段和方法)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
为什么我们不能在内部类中有静态方法?

我知道创建一个非静态内部类对象需要一个外部类对象,并且创建的非静态内部类对象自动具有对外部类对象的隐藏引用.但是为什么非静态内部类不能有静态成员呢?Java设计者只需要禁止内部类的静态方法内部的非静态外部类字段的访问,它会更有意义,非?

I know that the creation of a non-static inner-class object requires an outer-class object and the created non-static inner-class object automatically have a hidden reference to the object of the outer-class. But why can't a non-static inner-class have static members? The Java designer just have to disallow the access of non-static outer-class fields inside a static method of the inner-class, it would make more sense, non?

如果内部类有静态成员没有意义,为什么内部类可以通过继承有静态成员的类来继承静态成员?

If it does not make sense to have static members in an inner class, why inner class can inherit static members by inheriting a class who has static members?

我读了这个post 也是.如前所述:

I read this post too. As is mentioned:

内部类可以继承非编译时的静态成员常量,即使他们可能没有声明它们.嵌套类不是内部类可以自由声明静态成员,按照遵循 Java 编程语言的通常规则.

Inner classes may inherit static members that are not compile-time constants even though they may not declare them. Nested classes that are not inner classes may declare static members freely, in accordance with the usual rules of the Java programming language.

这是约定俗成的吗?

这是我的代码:

public class OuterClass {

    private int outerClassField;

    public void doSomethingOuterClass() {
        outerClassField = 1;
    }

    public static void doSomethingStaticOuterClass() {
        // outerClassField = 2; // Error: Because static method cannot access an specific object's field
    }

    public class InnerClass extends ClassWithStaticField {

        // Error: Why a non-static inner class cannot have static fields ?
        // public static int innerClassStaticField = 1;

        public void doSomethingInnerClass() {
            outerClassField = 3;
            staticField = 1;
        }

        // Error: Why a non-static inner class cannot have static methods ?
        // public static void doSomethingStaticInnerClass() {
        // outerClassField = 4;
        // }
    }

    public static void main(final String[] args) {
        // If it does not make sense to have static members in an inner class, why inner class can inherit statis members by inheriting a class who has static
        // members?
        OuterClass.InnerClass.staticField = 1;
        OuterClass.InnerClass.staticMethod();
    }
}

class ClassWithStaticField {
    public static int staticField;

    public static void staticMethod() {
    };
}

推荐答案

从技术上讲,我不知道为什么该语言限制对内部类使用静态元素的任何原因.非静态内部类可以通过使用将(以前的)封闭实例作为构造函数参数的普通类来模拟.当然,在可见性规则和可见性范围方面存在许多细微差别.

Technically there I don't know of any reason why the language restricts the use of static elements for inner classes. A nonstatic inner class can be emulated by using a normal class that takes the (formerly) enclosing instance as an argument to the constructor. Of course there are many little differences when it comes to visibility rules an visibility scopes.

我认为这是一个语言设计决定,主要是因为非静态内部类中的静态变量令人困惑且访问起来不直观 (Outer.Inner.StaticMember).

I presume it was a language design decision, mostly because statics in non-static inner classes are confusing and non-intuitive to access (Outer.Inner.StaticMember).

这篇关于为什么非静态内部类不能有静态成员(字段和方法)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆