Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

problem about anonymous inner class

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check out the following code
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class Wrapping{
public AnInterface aMethod(final int args){
return new AnInterface(){
private int i;
i = args
}
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
why should we declare the aMethod's argument as final, from the OO standpoint, what does the JVM benefit from doing such way?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code does not compile for several reasons. Do you mean something like this:

If you take the final out the compiler complains. It is a requirement that args is frozen so that the state of the object returned by aMethod() cannot be changed in unpredictable ways by external influences. The created object is only seeing a "snapshot" of the state of the local variables in the method invocation that creates it.
 
Peng Fan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first i must say thank you for the wonderful illustration and sorry for my poor coding which is due to carelessness. i would feel more easy if you could give some examples of the external influences that affect the state of the object returned by the aMethod() and whether your principle could also explain the reason why local inner class enclosed by a method can only have access to the final variables defined in the enclosing method.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i hope you are studying for an exam. Otherwise i would suggest to redesign and not use inner classes. It has nothing to do with the fact that it does not work, but more with pure readability and maintenance.
It is generally better to make your code readeable.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an example where a referenced local variable disappears:

[ May 08, 2004: Message edited by: Barry Gaunt ]
reply
    Bookmark Topic Watch Topic
  • New Topic