• Post Reply Bookmark Topic Watch Topic
  • New Topic

why only final type is permitted in annonymous class?  RSS feed

 
purushottam singh
Greenhorn
Posts: 10
MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why only final type objects / primitives are permitted in annonymous class?
Please explain..

Thanks in advance.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That only applies to local variables.

The point is that if it's a local variable, it's probably going to be out of scope at the time that the anonymous class code gets executed. The solution Java uses is that a copy of the local variable is taken. But that could lead to confusion if the value of the variable then changes. So they've added the restriction that the variable must be final, which means that you know the value never will change.

Instance variables and static variables will always be in scope for the anonymous class, so there's no problem there.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!