What is wrong with an inner class that doesn't use an outer class in Java?

I use static analyzer in Eclipse to examine my code. One class, foo, has an inner class, bar. I get the following error:

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

Why is this a mistake? As long as the outer class uses the inner class, is it not enough that this information is hidden useful and correct?

The inner class is not static.

+5
source share
5 answers

If the inner class can only be used by the outer class, but the inner class does not need a reference to the outer class, then you can do it private static.

- , , .

+7

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;
    }
  }
}

, - , static.
, .

// Correct
class Log {
  static class Position {
    private int line;
    private int column;
    Position(int line, int column) {
      this.line = line;
      this.column = column;
    }
  }
}
+11

, , . , . , "".

+7

. ( ) . , . static, , .

+5

, . , , .

+1

All Articles