jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes where final variable reside ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "where final variable reside ?" Watch "where final variable reside ?" New topic

where final variable reside ?

Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
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

SCJP1.5(81%), SCDJWS(94%), next mission SCEA (but need to wait or that)
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
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

Joined: Jul 05, 2007
Posts: 10
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...

Achievements are like fire flies, there is no point clinging on to them as they are not visible the next day morning.
I agree. Here's the link: http://aspose.com/file-tools
subject: where final variable reside ?
It's not a secret anymore!