In this session you will learn:
Inner Classes
For more information, visit this link: https://www.mindsmapped.com/courses/software-development/online-java-training-for-beginners/
3. Page 2Classification: Restricted
Objective
• Write code to construct instances of any concrete class including normal
top level classes, inner classes, static inner classes, and anonymous inner
classes.
4. Page 3Classification: Restricted
Inner Classes
• Inner classes can be non-static, static, method-local or anonymous
• An inner class instance has access to all the members of the outer class
including the private ones
Non-Static Inner Classes
• A regular or non-static inner class cannot have any static declarations
• The only way to access such a class is through an instance of the outer
class
5. Page 4Classification: Restricted
Example
Eg:
class MyClass {
private int i=10;
public void func() {
MyInner in=new MyInner();
in.callMe();
}
class MyInner {
public void callMe() {
System.out.println(“Value of i is “+i);
} }
}
6. Page 5Classification: Restricted
Instantiating an Inner Class
• To instantiate an inner class, you need an instance of the outer class
• From inside the outer class instance code, use the inner class name alone
to instantiate
Eg: MyInner myInner=new MyInner();
• From outside the outer class instance code, the inner class name must
include the outer class name
Eg: MyClass myClass=new MyClass();
MyClass.MyInner inner=myClass.new MyInner();
7. Page 6Classification: Restricted
Method local Inner Classes
• A method local inner class can be instantiated only within the method
where it is defined
• It cannot access the local variables of the enclosing method unless they
are final
Eg: class MyOuter {
private int i=2;
void callMe() {
class MyInner {
void callInner() {
System.out.println(“i is “+i);
}
}
} }
8. Page 7Classification: Restricted
Anonymous Inner Classes
• Anonymous inner classes have no name, and their type must be either a
subclass of the named type or an implementer of the named interface
Eg:
interface MusicalInstrument{ void play(); }
class Test{
MusicalInstrument m=new MusicalInstrument() {
public void play() {
System.out.println(“playing”);
} }; }
/* Instantiating an anonymous implementation class of the
MusicalInstrument interface */
9. Page 8Classification: Restricted
Static Nested Classes
• A static nested class is a static member of the enclosing class
• A static nested class does not have access to the instance variables and
methods of the class.
Eg:
class Outer{
static class MyNested{}
}
class Test{
public static void main(String args[]){
Outer.MyNested n=new Outer.MyNested();
}
}
10. Page 9Classification: Restricted
Extra Points to Remember…
• To refer to the inner class instance, use the keyword this from code
within the inner class. To reference the outer this, precede the keyword
this with the outer class name.
Eg: MyOuter.this
• The only modifiers that can be applied to a method local inner class are
abstract and final
• An anonymous subclass can extend a class or implement one interface,
not both.
• You don’t need an instance of the outer class to instantiate a static
nested class.