Classes/Objects  «Prev 


Inner Class

A Java inner class or nested class is a class declared inside a class or interface.
We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable. Additionally, it can access all the members of the outer class including private data members and methods.

You can define a class (or an interface) as a non-static member inside another class.
How about declaring a class or an interface inside an interface?
As you saw on the previous page "Types of Nested Classes", when you define a class or an interface inside an interface, it is implicitly static. Therefore, it is not possible to declare a non-static inner interface.
This leaves two possibilities:

class Outer { //1)  an outer class has an inner class
  class Inner {}
}
class Outer { //2)  an outer class has an inner interface
  interface Inner {}
}

Every inner class is associated with an instance of the outer class.
In other words, an inner class is always associated with an enclosing object.


The outer and inner classes share a special relationship. Member accesses are valid irrespective of the access specifiers such as private. However, there is subtle difference. You can access members of an outer class within an inner class without creating an instance; but this is not the case with an outer class. You need to create an instance of inner class in order to access the members (any members, including private members) of the inner class. One limitation of inner classes is that you cannot declare static members in an inner class, like this:

class Outer {
 class Inner {
  static int i = 7;
 }
}

If you try to do so, you will get the following compiler error:
Outer.java:3: inner classes cannot have static declarations static int i = 7;

Points to Remember

Here are some important rules about inner classes and interfaces that might are for the OCPJP 7 exam:
  1. The accessibility (public, protected, etc.) of the inner class is defined by the outer class.
  2. Just like top-level classes, an inner class can extend a class or can implement interfaces. Similarly, an inner class can be extended by other classes, and an inner interface can be implemented or extended by other classes or interfaces.
  3. An inner class can be declared final or abstract.
  4. Inner classes can have inner classes, but you will have a hard time reading or understanding such complex nesting of classes. In other words, do not write code that has inner classes within inner classes.

The following class shows you how to chain the Outer instance with the inner reference to
  1. access an instance variable of class Inner and
  2. call a method of class Inner .

class Outer {
 Outer() {} // constructor 1
 Outer(String s) {} // constructor 2
 class Inner {
  Inner() { } // constructor for the inner class
  int a = 3;
  void m1() {
   System.out.println("Within m1(): a= " + a);
  }
 }
 public static void main(String[] args) {
  Outer o1 = new Outer();
  Outer.Inner i1 = o1.new Inner();
  System.out.println("i1.a = " + i1.a);
  i1.m1();
 }
}
/*
i1.a = 3
Within m1(): a= 3
 */