5

I'm using a static analyzer in Eclipse to examine my code. One class, foo, has an inner class, bar. I am getting the following error:

JAVA0043 Inner class 'bar' does not use outer class 'foo' 

Why is this an error? As long as the outer class uses the inner class isn't that sufficient to make this information hiding useful and correct?

The inner class is not static.

3
  • @skaffman The inner class is not static. Commented Jul 15, 2009 at 19:23
  • @Ron Thanks! You are exactly righ. Commented Jul 15, 2009 at 19:28
  • 1
    note: static nested classes are NOT inner classes, by definition: java.sun.com/docs/books/jls/third_edition/html/… Commented Jul 16, 2009 at 11:17

5 Answers 5

10

Looks like an Enerjy Error:

// Incorrect class Log { // Position never uses the enclosing Log instance, // so it should be static class Position { private int line; private int column; Position(int line, int column) { this.line = line; this.column = column; } } } 

A nested class that does not use any instance variables or methods from any of its outer classes can be declared static.
This reduces the dependency between the two classes, which enhances readability and maintenance.

// Correct class Log { static class Position { private int line; private int column; Position(int line, int column) { this.line = line; this.column = column; } } } 
Sign up to request clarification or add additional context in comments.

Comments

7

If it's not making any reference to the outer class, it might as well be a full-on, regular class. Since it isn't dependent on the outer class, it can stand on its own. I suspect that's the reason for the "error".

3 Comments

I found this when I did a GOOG for 'JAVA0043 which reiterates what Carl said: enerjy.com/explorer/help/rules/JAVA0043.html
I understand that it can depend on its own, but if I want to practice good information hiding then I should keep it as an inner class. Right?
Yes; sometimes keeping it as a private static inner class may be a better option. But for testability, a top-level class may be preferable.
6

If the inner class can only ever be used by the outer class, yet the inner class needs no reference to the outer class, then you can make it private static.

If the inner class is known to someone other than the outer class, then it might as well be a top-level class in its own right.

2 Comments

@Christian: If you change the first phrase to: "If the inner class can only ever be used by the outer class..." I will make this the accepted answer.
Note Carlos Heuberger's comment in the original question. Also note that this error was probably generated by the Enerjy static analyzer -- see VonC's answer.
5

A non-static inner class has an implicit reference to an instance of its outer class. This hidden reference can delay (or even prevent) garbage collection on the outer class and create serialization issues. So you should only use non-static inner classes when you need them. It is easy to forget to declare the class static, so the code analysis warns you when it isn't needed.

Comments

1

The whole point of the inner class is that it has access to the outer class. If you're not actually using the outer class, just make it a regular, full-blown class.

1 Comment

If you aren't using outer class, make it a private static class.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.