views:

251

answers:

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.

+7  A: 

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

Carl Manaster
I found this when I did a GOOG for 'JAVA0043 which reiterates what Carl said: http://www.enerjy.com/explorer/help/rules/JAVA0043.html
seth
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.
Carl Manaster
+1  A: 

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.

Randolpho
If you aren't using outer class, make it a private *static* class.
notnoop
+11  A: 

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;
    }
  }
}
VonC
Yes, I believe that this error was generated by Enerjy.
+7  A: 

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.

Christian Vest Hansen
@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: 

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.

Yishai