我在 Eclipse 中使用静态分析器来检查我的代码。一个类 foo 有一个内部类 bar。我收到以下错误:
JAVA0043 Inner class 'bar' does not use outer class 'foo'
为什么这是一个错误?只要外部类使用内部类,还不足以使此信息隐藏有用且正确吗?
内部类不是静态的。
最佳答案
看起来像 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;
}
}
}
不使用任何外部类的任何实例变量或方法的嵌套类可以声明为静态。
这减少了两个类之间的依赖性,从而增强了可读性和可维护性。
// Correct
class Log {
static class Position {
private int line;
private int column;
Position(int line, int column) {
this.line = line;
this.column = column;
}
}
}
关于java - 在 Java 中不使用外部类的内部类有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1132930/