Possible Duplicate:
Why does Java prohibit static fields in inner classes?
Let's look at the following code snippet in Java. It just sums up two numbers within the Inner
class declared inside the Outer
class and works just fine as expected.
package staticfields;
final class Outer
{
final public static class Inner
{
private static int x;
private static int y;
public Inner(int x, int y)
{
Inner.x=x;
Inner.y=y;
}
public void sum()
{
System.out.println(x+y);
}
}
}
final public class Main
{
public static void main(String[] args)
{
new Outer.Inner(5, 10).sum();
}
}
When I attempt to remove the static keyword from the Inner
class, it issues a compile-time error indicating that inner classes can not have static declarations means that the the static fields (x and y) declared within the Inner class don't work, if it is made non-static.
Why do only static inner classes in Java have static members and non-static inner classes don't?