Inner Classes

C

Chase Preuninger

Does anyone know why you can only access final variables from within
an inner class inside a method? Also does anyone know why you can't
declare a static field inside of a regular inner class?
 
P

Patricia Shanahan

Chase said:
Does anyone know why you can only access final variables from within
an inner class inside a method? Also does anyone know why you can't
declare a static field inside of a regular inner class?

I believe the reason for the final variable only rule is to avoid
dealing with preserving the value of a changing local when its declaring
method has terminated. The inner class object can save the value of a
final variable during instance creation.

If non-final variables were allowed, but with a rule that the inner
class sees the value at instance creation time, you could have a
situation in which someone using a debugger sees one value of the
variable in the stack frame for the method, but the inner class is using
a different, older value.

In effect, the "final" is a reminder that post-creation changes in value
of the variable cannot affect the value the inner class object sees.

I have no idea about the static variable issue.

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top