Java: Access local variables from anon inner class? (PriorityQueue)

放肆的年华 提交于 2019-12-01 10:36:38
cletus

Yes, make it final:

public static Queue<String> topoSort(final DirectedGraph<String, DefaultEdge> g) {

See The Final Word On the final Keyword:

Anonymous Local Classes

The second situation involving final variables is actually mandated by language semantics. In that situation, the Java compiler won't let you use a variable unless it is declared final. This situation arises with closures, also known as anonymous local classes. Local classes can only reference local variables and parameters that are declared final.

public void doSomething(int i, int j)
{
  final int n = i + j; // must be declared final

  Comparator comp = new Comparator()
  {
    public int compare(Object left, Object right)
    {
      return n; // return copy of a local variable
    }
  };
} 

The reason for this restriction becomes apparent if we shed some light on how local classes are implemented. An anonymous local class can use local variables because the compiler automatically gives the class a private instance field to hold a copy of each local variable the class uses. The compiler also adds hidden parameters to each constructor to initialize these automatically created private fields. Thus, a local class does not actually access local variables, but merely its own private copies of them. The only way this can work correctly is if the local variables are declared final, so that they are guaranteed not to change. With this guarantee in place, the local class is assured that its internal copies of the variables accurately reflect the actual local variables.

If a local variable is used in an inner class, the compiler generates a instance field for the inner class and copies the local variables value to it. Now, if the local variable changes then the instancs variable value will still be having old value. So, in order to make the local variables value same as instance field and not mutable, it is made final.

FYI, with google collections you also have the option of

  Ordering.natural().onResultOf(
      new Function<String, Integer>() {
        public Integer apply(String arg) {
          return g.inDegreeOf(arg);
        }
      });

This doesn't necessarily save you a lot of typing, as you can see, unless you can find other uses for that Function, but it does shield you from the bugs that very often creep into your by-hand comparator implementations.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!