java - Why the list must be declare by final -
java - Why the list must be declare by final -
tintarraylist list = new tintarraylist(); final tintarraylist templist = new tintarraylist(); list.add(10086); list.add(12345); list.add(1989); list.foreach(new tintprocedure() { @override public boolean execute(int i) { if (i > 10086) { templist.add(i); } homecoming true; } });
i utilize intellij, , prompts me declare templist final,why templist has declared final?
it's because of way virtual machine works.
first, understand this, need know internal stack , stack frames (inside virtual machine)
local variables (both primitives , references) stored in method's stack frame , not accessible other methods.
in case, local variable templist
not accessible in method boolean execute(int i)
because "belongs" enclosing method (it "lives" in local stack frame).
but, create accessible declare variable final, way internally set "outside" of method, if private instance variable, can accessed execute()
, other methods.
java trove4j
Comments
Post a Comment