• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Inner Classes

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When considering the inner classes .we must modify variables using "final" keyword .why is that?
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

lahiru nanayakkara wrote:
When considering the inner classes .we must modify variables using "final" keyword .why is that?



Those are words that could imply many things. Just so I'm sure about what you're asking, could you post a code snippet that explains your question?

The java compiler does not allow us to reassign a different object/value to a final reference, after it has been initialized. So the 'modification' ( direct modification through reassignment ) as such is not possible. Mutations ( here I mean the change of state ), however, are a different story.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chan Ag wrote:

lahiru nanayakkara wrote:
When considering the inner classes .we must modify variables using "final" keyword .why is that?



Those are words that could imply many things. Just so I'm sure about what you're asking, could you post a code snippet that explains your question?

The java compiler does not allow us to reassign a different object/value to a final reference, after it has been initialized. So the 'modification' ( direct modification through reassignment ) as such is not possible. Mutations ( here I mean the change of state ), however, are a different story.



Actually, to be precise, reassignments of any kind to a final reference are illegal. The second line in the below code, for example, is illegal.



But that is the property of all final references.
 
Marshal
Posts: 76873
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe it means that local variables used in an anonymous class must be final.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps. But let's hear him/her. :-)
 
lahiru nanayakkara
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what is the question here? That should compile and give you an output. Are you asking why class B can access the variable 'local' only if it is final?

If so, this question has been asked quite a few times and here's one post that might help you.

https://coderanch.com/t/623236/java-programmer-SCJP/certification/Method-local-classes#2849294

There are quite a few others as well in this forum. You can try searching for words like 'method local inner classes' or something.

By the way, it is not right to use the word 'modify' when you imply 'access' ( of course I'm guessing that you implied access there ).
 
Campbell Ritchie
Marshal
Posts: 76873
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I presume you have been through this Java Tutorials section?
reply
    Bookmark Topic Watch Topic
  • New Topic