Why a non-static inner-class cannot have static me

2020-02-05 08:17发布

问题:

This question already has answers here:
Closed 7 years ago.

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

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:

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.

Is it a convention?

Here is my code:

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() {
    };
}

回答1:

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.

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).



回答2:

1. An object of a Non-static inner class is associated with an instance/object of its Outer Class (ie enclosing class).

2. The entire body of a Non-static inner class is Not within a static scope, and therefore you can't have static members in there.

3. Instance variables and methods in the non-static inner class are relative to an instance of the enclosing class, so being related to an object, static won't hold true for them (ie inner class),

4. When we create an Instance of non-static inner class, we Need an Object of Outer enclosing class. The innernon-static class has an implicit reference to it outer enclosing class.

Eg:

Outer o = new Outer();
Outer.Inner i = o.new Inner();


回答3:

There is no point of providing a static method inside a non static inner class. You could use a non static method inside the outer class instead ?