• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

final in anonymous inner class

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
vignesh hariharan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no.. still confusing.. some help me out..
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
vignesh hariharan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you people for taking time to clarify my doubts...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic