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

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

问题描述

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

Possible Duplicate:
Why cant we have static method in an inner class?

我知道创建一个非静态内部类对象需要一个外部类对象,并且创建的非静态内部类对象会自动具有对该外部类对象的隐藏引用.但是为什么非静态内部类不能具有静态成员呢? 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?

我阅读了此

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天全站免登陆