Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

where final variable reside ?

 
Divya Gehlot
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have confusion that if declare a variable or object refernce as final then where they reside. As I was going through Inner classes topic and its like if declare variable as final Inner clas is able to access but not normal variable.

Compiling the preceding code really upsets the compiler:
MyOuter2.java:8: local variable z is accessed from within inner class;
needs to be declared final
System.out.println("Local variable z is " + z);
^
Can any one exaplein why it is so?
Thanks in advance
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Divya Gehlot:
Can any one exaplain why it is so?

Since it is specified this way in the JLS. Quote from 8.1.3 Inner Classes and Enclosing Instances.
Any local variable, formal method parameter or exception handler parameter used but not declared in an inner class must be declared final. Any local variable, used but not declared in an inner class must be definitely assigned (�16) before the body of the inner class.
 
Deepinder Singh
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
method local inner classes have access to member variables of the enclosing class.. if a local variable needs to be used it should be declared final....try writing a small program...if your into software ...you will find it used as part of IBatis ORM mapping tool usage...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic