Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Virtual Machine / May 2005

Tip: Looking for answers? Try searching our database.

Nested / Inner class in bytecode

Thread view: 
nico - 12 May 2005 18:54 GMT
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
Chris Uppal - 13 May 2005 13:31 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.