Mystery of Accessibility in Local Inner Classes

In Java, I’d always known that whenever I wanted to pass a local variable into a (possibly anonymous) inner class without any getter/setter code I had to declare the variable as final, or alternatively it had to be an instance variable of the parent class.

If you just read that sentence and had no idea what I said, please feel free to skip the rest of this post.

What I didn’t know was why this was necessary. Clearly the assumption is that Java does some trickery, but needs the variable to be a constant value. Then I read this blog post and learnt that essentially that is what happens. Perhaps more interesting is that inner classes are only handled at the compiler level, in other words the JVM is completely oblivious to their existence.

If you are a Java dev and have a few minutes spare, this blog post is very interesting. I’m always interested in learning new aspects of the Java language/runtime, and I have a feeling I’m going to need to find a book on Java bytecode sooner or later.

0 Responses to “Mystery of Accessibility in Local Inner Classes”


  • No Comments

Leave a Reply

About

Jonathan Giles is a 25 year old software engineer living in Thames, New Zealand. He holds a Bachelor of Engineering Honours in Software Engineering, a Masters of Science in Computer Science, and is a Sun certified Java programmer. Jonathan specialises in Java, Swing, JavaFX and Client-Server development.

He is currently a software engineer at Oracle in the JavaFX UI controls team. He also blogs over at the FX Experience blog. Obviously, the opinions expressed here are his own.

Contact

Email:   Here
NZ:   +64 211 089 038
US:   +1 408 372 8057
Twitter:   JonathanGiles
LinkedIn:   My Profile
Skype:   Skype Me