> In Java, is the nested/inner class only "syntactic sugar" or is there
> particular statements in the bytecode ?
As far as the actual bytecodes go, nested classes are pure syntactic sugar.
The JVM runtime doesn't know anything about them, and doesn't realise that they
are not top-level classes -- indeed they /are/ top-level classes in everything
but the Java syntax for creating them.
But there is information in the classfile format that is not used by the JVM
runtime. That records the relationship between nested classes and their outer
classes (including things like whether they are public or private, etc). That
information is used by javac when compiling against another classfile. (And is
probably also used by the reflection stuff -- but I'm not sure about that.)
-- chris
Ian Rogers - 13 May 2005 14:18 GMT
>>In Java, is the nested/inner class only "syntactic sugar" or is there
>>particular statements in the bytecode ?
[quoted text clipped - 3 lines]
> are not top-level classes -- indeed they /are/ top-level classes in everything
> but the Java syntax for creating them.
It is the case that an inner class can access private fields of an outer
class. I can't imagine they are marked public by javac, so the jvm must
know something special is going on with private fields and inner class
accesses through their synthetic outer class member field. Possibly of use:
http://c2.com/cgi/wiki?InnerClasses
Ian
Chris Uppal - 13 May 2005 16:06 GMT
> It is the case that an inner class can access private fields of an outer
> class. I can't imagine they are marked public by javac, so the jvm must
> know something special is going on [...]
Not true. Not a bad bit of reasoning, but you are underestimating the
grossness of the hacks that have been introduced into javac over the years ;-)
The compiler just generates so-called "synthetic" accessors with
package-default protection. Then it translates accesses of private variables
into calls to those methods. It also does something similar for private
methods and constructors.
You don't see those methods in the Java source, but they are there in the
classfiles.
-- chris