Java Glossary : inner classes

CMP home Java glossary home Menu no menu 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.

inner classes
A feature of Java 1.1+ where nested local classes are allowed to be defined inside enclosing classes and create possibly anonymous classes and objects.

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.

When To Use Inner Classes

Inner classes are ugly. They have no identifying inner keyword to note you to their presence. It is so easy to miss them buried. You have to look at the {} very carefully to decide if they are truly inner classes or just non public classes sharing the same source file, i.e. top level classes. So why would you want them? If you find that you are passing a ton of stuff in the the constructor to a class and that class is only used by one other class, inner classes may come to the rescue, by giving direct reference to all the mother class's fields. Similarly if you find you are doing a huge amount of referencing of the mother classes fields, think of inner classes. Inner classes can also take a snapshot in time of the final local variables of a mother class's calling method. To develop intuition, try coding both ways and see which comes out cleaner.

Instantiating

Inside the enclosing class, you can instantiate inner classes with the ordinary syntax. You can instantiate a public inner class outside the outside class with syntax like this:

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" );


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
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