14

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() {
    };
}
Community
  • 1
  • 1
Kewei Shang
  • 949
  • 3
  • 11
  • 27
  • See also : http://stackoverflow.com/questions/2482327/why-can-we-have-static-final-members-but-cant-have-static-method-in-an-inner-cla – Autar Jul 27 '12 at 09:19

3 Answers3

17

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();
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • 2
    Regarding point 2: Static compile-time constant fields *are* allowed within inner classes – Zippy Jul 16 '18 at 00:10
  • static compile time constants are not static members. They are, as the name suggest, constants and thus remain same across all the instances of enclosing class and therefore, probably are allowed. – Yug Singh Oct 02 '18 at 11:38
  • The interesting thing is you can use static final together in the inner class. – Vincent Oct 17 '18 at 00:58
  • 1
    @Vincent only if this `static final` field is a compile-time constant (prior to JDK 16), which means it had to be of a primitive type or string and required a constant expression as initializer. Starting with JDK 16, you can have all sorts of `static` members. – Holger May 04 '22 at 15:14
12

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

Durandal
  • 19,919
  • 4
  • 36
  • 70
  • 2
    Correct. There’s no technical restriction and [a new design decision has been made](https://bugs.openjdk.java.net/browse/JDK-8254321). – Holger May 04 '22 at 15:07
0

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 ?

Achilles
  • 1,065
  • 2
  • 13
  • 29
  • 1
    "no point" is not an answer and also does not address the issue of member data in addition to methods. – RichieHH Mar 08 '14 at 22:58