AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Nested / Inner class in bytecode

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> JVM, native methods and hardware
View previous topic :: View next topic  
Author Message
nico
Guest





PostPosted: Thu May 12, 2005 5:54 pm    Post subject: Nested / Inner class in bytecode Reply with quote



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





PostPosted: Fri May 13, 2005 12:31 pm    Post subject: Re: Nested / Inner class in bytecode Reply with quote



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





PostPosted: Fri May 13, 2005 1:18 pm    Post subject: Re: Nested / Inner class in bytecode Reply with quote



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





PostPosted: Fri May 13, 2005 3:09 pm    Post subject: Re: Nested / Inner class in bytecode Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> JVM, native methods and hardware All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.