GeeCON Prague 2014*
The moose likes Java in General and the fly likes final in anonymous inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "final in anonymous inner class" Watch "final in anonymous inner class" New topic
Author

final in anonymous inner class

vignesh hariharan
Ranch Hand

Joined: Jun 23, 2005
Posts: 77
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..

pls help me out..


Regards,
vignesh
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
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."

From: http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html

As to why this is a rule of the language specification, I'll leave that for someone else.


Fun programming etcetera!
vignesh hariharan
Ranch Hand

Joined: Jun 23, 2005
Posts: 77
no.. still confusing.. some help me out..
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

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".

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
vignesh hariharan
Ranch Hand

Joined: Jun 23, 2005
Posts: 77
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..
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.
vignesh hariharan
Ranch Hand

Joined: Jun 23, 2005
Posts: 77
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 ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Is there some other reason why local variables are ever made final? That's what "final" means in this context: you're not allowed to change them.
vignesh hariharan
Ranch Hand

Joined: Jun 23, 2005
Posts: 77
thank you people for taking time to clarify my doubts...
 
GeeCON Prague 2014
 
subject: final in anonymous inner class