Inner Classes
Neelam A
[Link]
Copyright © 2005 – 2007, Edujini Labs Pvt. Ltd. All rights reserved.
Overview
• Kinds of classes
• Inner
• Nested
• Inner classes
• Anonymous inner classes
• Nested classes
• Local Class
[Link]
Copyright © 2005 – 2007, Edujini Labs Pvt. Ltd. All rights reserved.
Kind of Classes
• Top level classes
• Declared inside package
• Visible throughout package, perhaps further
• Normally declared in their own file
Public classes must be defined in their own file
Not required for other classes
• Inner and nested classes
• Declared inside class (or method)
• Visible normally only to outer class
Can have wider visibility
[Link]
Copyright © 2005 – 2007, Edujini Labs Pvt. Ltd. All rights reserved.
Kind of Inner/Nested Classes
• Inner class
• Anonymous inner class
• Nested class
• Examples
public class MyOuterClass {
public class MyInnerClass { … }
static public class MyNestedClass { … }
Iterator iterator( ) { return new Iterator( ) { … } }
}
[Link]
Copyright © 2005 – 2007, Edujini Labs Pvt. Ltd. All rights reserved.
Inner Classes
• Description
• Class defined in scope of another class
• Property
• Can directly access all variables & methods of
enclosing class (including private fields & methods)
• Example
public class OuterClass {
public class InnerClass {
...
}
}
[Link]
Copyright © 2005 – 2007, Edujini Labs Pvt. Ltd. All rights reserved.
Inner Class
• May be named or anonymous.
• Useful for
• Logical grouping of functionality
• Data hiding
• Linkage to outer class
• More readable/maintainable code
• Examples
• Iterator for Java Collections
• Listeners for Java GUI Applications
[Link]
Copyright © 2005 – 2007, Edujini Labs Pvt. Ltd. All rights reserved.
Inner Classes
• Inner class instance
• Has association to an instance of outer class
• Must be instantiated with an enclosing instance
• Is tied to outer class object at moment of creation (can not be
changed)
[Link]
Copyright © 2005 – 2007, Edujini Labs Pvt. Ltd. All rights reserved.
Inner Classes Example
• Code
public class OC { // outer class
private int x = 2; // don’t forget private
public class IC { // inner class
int z = 4;
public int getSum() {
return x + z;
}
}
}
[Link]
Copyright © 2005 – 2007, Edujini Labs Pvt. Ltd. All rights reserved.
Inner Class Example
• Class referencing syntax
• [Link]
• Example
OC oc = new OC();
[Link] ic; // name of inner class
// ic = new [Link]() doesn’t work!
ic = [Link] IC(); // instantiates inner class
// ic now will "know about" oc, but not vice versa
[Link]() yields 6 // can access private x in oc!
[Link]
Copyright © 2005 – 2007, Edujini Labs Pvt. Ltd. All rights reserved.
Creating/Referring to Inner Classes
• Assume class A defines an inner class B.
• Inside instance methods of A, just use B as the type of
references to the inner class and use new B(…) to create
instances
• Newly created B object associated with A object
referenced by this
• Outside of A, use A.B to name the inner class.
• If you need to create an instance of B associated with a
specific A object a, outside of an instance method on a
• Use [Link] B()
• It is very rare for you to need to do this
[Link]
Copyright © 2005 – 2007, Edujini Labs Pvt. Ltd. All rights reserved.
Accessing Outer Scope
Code
public class OC { // outer class
int x = 2;
public class IC { // inner class
int x = 6;
public void getX() { // inner class method
int x = 8;
[Link]( x ); // prints 8
[Link]( this.x ); // prints 6
[Link]( [Link].x ); // prints 2
}
}
}
[Link]
Copyright © 2005 – 2007, Edujini Labs Pvt. Ltd. All rights reserved.
Nested Class
• Declared like a standard inner class, except you say “static
class” rather than “class”.
• For example,
class LinkedList {
static class Node {
Object head;
Node tail;
}
Node head;
}
[Link]
Copyright © 2005 – 2007, Edujini Labs Pvt. Ltd. All rights reserved.
Nested Classes
• A static nested class is behaviorally a top-level class that has
been nested in another top-level class for packaging
convenience.
• An instance of a nested class does not contain an implicit
reference to an instance of the outer class.
• Still defined within outer class, has access to all the private fields
• Use if inner object might be associated with different outer
objects, or survive longer than the outer object
• Or just don’t want the overhead of the extra pointer
in each instance of the inner object
[Link]
Copyright © 2005 – 2007, Edujini Labs Pvt. Ltd. All rights reserved.
Anonymous Inner Class
• Doesn’t name the class.
• Inner class defined at the place where you create an
instance of it (in the middle of a method).
• Useful if the only thing you want to do with an inner
class is create instances of it in one location
• In addition to referring to fields/methods of the outer
class, can refer to final local variables.
[Link]
Copyright © 2005 – 2007, Edujini Labs Pvt. Ltd. All rights reserved.
Anonymous Inner Class
Syntax:
new ReturnType() { // unnamed inner class
body of class… // implementing ReturnType
};
[Link]
Copyright © 2005 – 2007, Edujini Labs Pvt. Ltd. All rights reserved.
Local Inner Class
We can declare an inner class within the body of a method.
Such a class is known as a local inner class.
[Link]
Copyright © 2005 – 2007, Edujini Labs Pvt. Ltd. All rights reserved.