• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner Classes and Java Files  RSS feed

 
Steve Stanicki
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am working thru Head First, and I created the BeatBox. When I compiled, I noticed that the inner classes are now class files in their own right.

I have one file for each listener class, and they have a '$' as part of their name:

BeatBoxt$MyStartListener.class

Head First doesn't seem to go into this phenomenon.

Could someone explain this any further? What happens?

Thanks,
Steve
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With nested classes like MyStartListener you see that Java allows more than one class per .java source file. However it is strictly one compiled class per .class file. The $ is a bit of punctutation used in naming these files -- if the file had just been called MyStartListener.java it may have conflicted with another class with that name, perhaps a top-level class or another nested class.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps HFJ doesn't go into detail on this because there really isn't much to explain here. This is the convention that Java uses to create .class files for inner class. In general, the .class file is named as "OuterClass$InnerClass.class" after it is compiled. If you use anonymous inner classes, then the inner class is given a numerical name such as "OuterClass$1.class".

Layne
 
karthikeyan Chockalingam
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same phenomenon can be observed when compiling swing related Java file(s)
[ January 19, 2006: Message edited by: karthi keyan ]
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by karthi keyan:
The same phenomenon can be observed when compiling swing related Java file(s)

It doesn't have anything to do with Swing per se. it is because you are defining nested classes, either named (A$B.class) or anonymous (A$1.class).
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To give some background: it is in fact the case that the JVM that loads and executes the class files doesn't know anything about nested or inner classes. In byte code, every class is a top level class.

So what the compiler does when it finds an inner class in the source code is that it uses some tricks, plus the naming convention you observed, to transform then into regular top level classes.

With other words, inner classes are not much more than syntactic sugar.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s/inner class/nested class

Replace all occurrences of 'inner class' with 'nested class' in this thread.
http://jqa.tmorris.net/GetQAndA.action?qids=67&showAnswers=true
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!