 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
nico Guest
|
Posted: Thu May 12, 2005 5:54 pm Post subject: Nested / Inner class in bytecode |
|
|
Hello,
My question is simple but I didn't find a clearly answer (in particular
in the "JVM specification" and in the "Java Language Specification") :
In Java, is the nested/inner class only "syntactic sugar" or is there
particular statements in the bytecode ?
Thanks in advance,
Nico
|
|
| Back to top |
|
 |
Chris Uppal Guest
|
Posted: Fri May 13, 2005 12:31 pm Post subject: Re: Nested / Inner class in bytecode |
|
|
nico wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
Ian Rogers Guest
|
Posted: Fri May 13, 2005 1:18 pm Post subject: Re: Nested / Inner class in bytecode |
|
|
Chris Uppal wrote:
| Quote: | nico wrote:
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.
|
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
|
|
| Back to top |
|
 |
Chris Uppal Guest
|
Posted: Fri May 13, 2005 3:09 pm Post subject: Re: Nested / Inner class in bytecode |
|
|
Ian Rogers wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|