3

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?

4
  • 5
    stackoverflow.com/questions/1953530/… The second answer is better: stackoverflow.com/a/1953570/738746 Commented Dec 20, 2011 at 0:05
  • @glowcoder:) The error is issued when you remove the static keyword from the inner class and make it a non-static class. Did you try to do so? Commented Dec 20, 2011 at 0:06
  • @Lion Yeah, the problem lied in that I had another class named Outer in that directory, so I called this one OuterTest, but was still compiling Outer :) Whoopsie :) Commented Dec 20, 2011 at 0:08
  • There's no such thing in Java as a static inner class. By definition an inner class is nested but non-static. The class you named Inner is a static nested class, not an "inner class" and certainly not a "static inner class". This makes your question extremely confusing to read because you're referring to an Inner class (that's how you named it) that is not at all an inner class : ) Commented Dec 20, 2011 at 0:11

3 Answers 3

0

An inner class is a nested class that is not explicitly or implicitly declared static. Inner classes may not declare static initializers (§8.7) or member interfaces. Inner classes may not declare static members, unless they are compile-time constant fields"

Inner class belong to an instance of the out class, so it makes sense.

Sign up to request clarification or add additional context in comments.

Comments

0

Because "inner classes can not have static declarations". :-)

But seriously, inner class is non-static by definition. You need to understand difference between inner and nested classes (static).

Comments

0

First, a quick recap of why you can declare an inner class to be static:

A non-static inner class can only exist within the context of an instance of its declared parent class. So if your class Inner was non-static, then it can only ever exist within a particular instance of Outer. I.e. Inner depends on having an instance of Outer, and each instance of Inner "refers" to an instance of Outer (using the word refers here loosely).

This allows a non-static inner class to reference non-static fields and methods of Outer without explicitly using a reference to Outer.

In order to tell the compiler and JVM that this is not the desired behaviour of the inner class, you need to make the inner class static (so it is no longer a "inner" class). This then breaks the dependency of the inner class to outer class instance, and lets you declare the "inner" (nested) class just like it was any other regular Java class.

Now, since a non-static inner class depends on a specific instance of the outer class, it does not make sense for this inner class to have non-final static members (since modifying one effectively breaks the encapsulation of the inner class). I am sure they probably could technically allow it if they wanted to, but it would probably be bad programming practice to use it, and indicate that what you really wanted was a static nested class anyways.

1 Comment

I think it will be more readable to update the terms you used, by refering to: docs.oracle.com/javase/tutorial/java/javaOO/nested.html , since an inner class already implies it's non-static...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.