Possible duplicate:
Why can't we use the static method in the inner class?
I know that to create a non-static object of an inner class, an object of an external class is required, and the created non-static object of the inner class automatically has a hidden link to the object of the outer class. But why does a non-static inner class have static elements? A Java developer just has to deny access to non-static fields of an outer class inside a static method of an inner class, this will make more sense, no?
If it makes no sense to have static members in an inner class, why can an inner class inherit static members by inheriting a class that has static members?
I read this post . As already mentioned:
Inner classes can inherit static members that are not compilation although they cannot declare them. Nested classes that are not inner classes can freely declare static members in accordance with the usual rules of the Java programming language.
Is this an agreement?
Here is my code:
public class OuterClass {
private int outerClassField;
public void doSomethingOuterClass() {
outerClassField = 1;
}
public static void doSomethingStaticOuterClass() {
}
public class InnerClass extends ClassWithStaticField {
public void doSomethingInnerClass() {
outerClassField = 3;
staticField = 1;
}
}
public static void main(final String[] args) {
OuterClass.InnerClass.staticField = 1;
OuterClass.InnerClass.staticMethod();
}
}
class ClassWithStaticField {
public static int staticField;
public static void staticMethod() {
};
}
source
share