Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why a non-static inner-class cannot have static members (fields and methods)? [duplicate]

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() {
    };
}
like image 549
Kewei Shang Avatar asked Jul 27 '12 09:07

Kewei Shang


2 Answers

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();
like image 123
Kumar Vivek Mitra Avatar answered Oct 05 '22 23:10

Kumar Vivek Mitra


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

like image 21
Durandal Avatar answered Oct 05 '22 21:10

Durandal