Last updated 2004-06-28 by Roedy
Green ©1996-2004 Canadian Mind Products
Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
You are here : home : Java Glossary : I words : inner classes.
It gets rather complicated. Are the classes sharing a source file nested or distinct? Separate ones are called top-level classes. Nested ones are called, well nested. There are two flavours of nested classes, static (which can be allocated independently of any particular outer class object), and inner classes which are always allocated in conjuction with some particular outer class object.
Just to make matters even more confusing, most programmers refer to both static nested classes and inner classes as inner classes.
The word static is so badly abused in Java. It has so many meanings, none of them anything like its meaning in ordinary English.
Dick Baldwin has written an essay explaining them. You can define an anonymous class, create an anonymous instance of it, and pass it as a parameter to a some method all in one (albeit very long) line. They are primarily for use by code generators. If you want to write really hard to follow code, you can nest inner classes within inner classes. You can refer to this of the outer class via MyOuterClass. this. You can refer to the outer class's methods by myOuterInstanceMethod() or MyOuterClass.this.myOuterInstanceMethod(). You might imagine that inner classes are merely a scope mechanism, that they are independent classes, visible only to the outer class. Not so. For every inner class object there must exist an associated outer class object. You can't create inner class objects without first creating an outer class object. You must create inner class objects in instance methods so that this implicitly gives you the corresponding outer class object.
If you declare the nested class static, it can be independently instantiated.
To instantiate an inner class given that you have an instance t of the outer class, you would type t. new MyInnerClass(). Inside instance methods of the outer class, you create new inner class instances more simply with: new MyInnerClass().
One way of thinking of inner classes in that their constructors get passed a hidden parameter, a reference to the outer class object that created them. static inner classes don't have this hidden parameter. This is analogous to the way assembler programmers look at instance methods having a hidden this parameter, where static method do not.
Outer.Inner in = out.new Inner();
You can instantiate an independent public static nested class like this where NumberEditor is a static nested class of JSpinner.
JSpinner.NumberEditor e = new JSpinner.NumberEditor ( year, "0000" );
home |
Canadian Mind Products | |||
| mindprod.com IP:[24.87.56.253] | ||||
| Your IP:[80.134.30.163] | ||||
| You are visitor number 5315. | ||||
| Please send errors, omissions and suggestions | ||||
| to improve this page to Roedy Green. | ||||
| You can get a fresh copy of this page from: | or possibly from your local J: drive mirror: | |||
| http://mindprod.com/jgloss/innerclasses.html | J:\mindprod\jgloss\innerclasses.html | |||