• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner Classes  RSS feed

 
lahiru nanayakkara
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When considering the inner classes .we must modify variables using "final" keyword .why is that?
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Perhaps. But let's hear him/her. :-)
 
lahiru nanayakkara
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • 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.

http://www.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: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you have been through this Java Tutorials section?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!