Inner Class Tutorial

The inner class is one of the four nested classes that I briefly discussed in my Introduction to Nested Classes tutorial. The inner class is defined at the member level and does not have the keyword static in the declaration. By member level I am talking about the same level where instance variables and methods are declared.
class Outer {
     String s = "";
     class Inner {
         int i = 0;
     }
}

The first thing to understand about an inner class is that it is a member of the class that is contained in. Therefore, you can only access the inner class through an instance of the class that it is contained in. Let's imagine that the Outer class above is contained in a file called Outer.java. When we compile the source code file we will end up with two class files: Outer.class and Outer$Inner.class. The Outer$Inner.class file is named so because Inner is a member of Outer and Inner cannot be accessed without an instance of Outer.

Here are some rules for an inner class:

  • An inner class can access all members of the outer class (even private).
  • An inner class can have all levels of access applied (public, protected, private and default).
  • An inner class can be final or abstract.
  • An inner class can extend other classes and implement interfaces.
  • An inner class can be instantiated using .new syntax.
  • An inner class is not a subclass of the outer class - no inheritance.
  • An outer class is not a superclass of the inner class - no inheritance.
  • An inner class cannot declare static variables or static methods.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md InnerClass
C:\Java>cd InnerClass
C:\Java\InnerClass>Notepad InnerClass.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class Tester {
    public static void main(String args[]) {
        Outer outer = new Outer();
        outer.displayST();

        Outer.Inner inner = outer.new Inner();
        inner.displayST();
	
        Outer.Inner oi = new Outer().new Inner();
    }
}

class Outer {
    private String s = "Outer s";
    private String t = "Outer t";
    void displayST() {
        System.out.println(s + ", " + t);
        System.out.println();
    }

    class Inner {
        String s = "Inner s";
        String t = "Inner t"; 
        void displayST() {
            System.out.println(s+", "+t);
            System.out.println(this.s+", "+this.t);
            System.out.println(Outer.this.s+", "+Outer.this.t);
        }    
    }
}


/////////////////

class Tester2 {
    public static void main(String args[]) {
        //Inner inner = new Inner();
        Tester2.Inner inner = new Tester2().new Inner();
        inner.displayS();

        new Tester2().interestingMethod();

        Tester2.Inner ti = new Tester2().getInnerReference();
        ti.displayS();
    }
	
    class Inner {
        String s = "whatever";
        void displayS() {
            System.out.println(s);
        }
    }

    void interestingMethod() {
        Inner inner = new Inner(); //works from non-static method
        inner.displayS();
    }

    Inner getInnerReference() {
        return new Inner(); //works from non-static method
    }
}


Now switch back to the command prompt (CMD) and type in javac InnerClass.java and press Enter.
Now type in java InnerClass and press Enter.


C:\Java\InnerClass>javac InnerClass.java
C:\Java\InnerClass>java InnerClass
Outer s, Outer t

Inner s, Inner t
Inner s, Inner t
Outer s, Outer t

and 

whatever
whatever
whatever


Final thoughts

You can only access the inner class through an instance of the outer class that it is contained in. The this keyword is instrumental in accessing members of an outer class from within the inner class. Static is not allowed in the inner class declaration or anywhere inside of the inner class body. Doing so makes the nested class a static nested class, not a regular inner class.


Tutorials