it seems that the methods which accepts arguments should accept arguments of final specifiers if the variables are to be accessed inside the innerclass which is defined inside the method.. why is that the argumetns are compulsarily be final?? or else it is throwing an error..
Well, one answer is to tell you that the rule is just part of the specification of the Java language.
"Any local variable, formal method parameter or exception handler parameter used but not declared in an inner class must be declared final, and must be definitely assigned (�16) before the body of the inner class."
What's confusing? The specification says it's so, therefore it's so.
In fact, I think the requirement for "final" is to do with how the anonymous inner classes are implemented internally. They have fields representing those parameters, which can be implemented more efficiently, safely and/or easily if they're "final".
But really, who cares? The answer is to accept it and move on to something more interesting.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
I believe the main reason for the "final" restriction is because those variables are on the stack, they can go out of scope before the inner class is done with them. By making them final, the inner class can make a copy of those variables that it uses.
Originally posted by vignesh hariharan: no.. still confusing.. some help me out..
Well, there is nothing confusing about the previous post. Dave is pretty much saying "because the specification says so".
even the specification is also fabricated with certain reasons.. java is not something which can jus be taken as by-hearting... there should be some reason behind that var being made final right?? and thanks henry that was acceptable.. since var lives in stack.. thanks for that..
and mean while i got few ideas.. jus help me out.. if it could be right... the reason is if it is not final then the value can be modified inside the anonymous innerclass which is not advisible.. so it should be final so that value sent to that method remains constant throughout the anonymous innerclass and inside the method.. am i right to some extent?? help me out..
Originally posted by vignesh hariharan: am i right to some extent?? help me out..
No. The reason is 100% what Henry says: if the variables weren't final, you'd expect changes to be reflected both inside the method and inside the code in the anonymous class; but the truth is that the anonymous class is working with copies of the variables. By making them final, we have the illusion that the anonymous class is working with the local variables themselves.
This is a topic that confuses me also, although I usually just take it as a matter of faith that it "just has to be".
...if the variables weren't final, you'd expect changes to be reflected both inside the method and inside the code in the anonymous class...
When you say this, what kind of changes are you talking about? Changing the object that the variable is referencing (i.e. myVar = otherFoo;) , or mutating the object that the variable is referencing (i.e. myVar.setBar(new Bar());) ? [ March 20, 2006: Message edited by: Garrett Rowe ]
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
author and iconoclast
If you think in terms of the local variables being primitives, then you don't have this complication! But if the variables weren't final, you'd expect to be able to mutate the references themselves. With final variables, you can only change the objects, but that is fine -- both the method and the anonymous class will be working on the same object, albiet through two different copies of the reference to it.
Joined: Jun 23, 2005
means are they made final to avoid mutation?? inside the anonymous innerclass and method?? is that the only motive that they r made final??? [ March 20, 2006: Message edited by: vignesh hariharan ]
author and iconoclast