1, define in a class inside the class we are called inner classes.
Second, the role of
1. Inside the implementation class is well hidden, that is encapsulation. General non-internal classes, is not allowed to have private and protected access, but the inner classes can be
2. Inner class outside the class has access to all the elements
3. However, to achieve multiple inheritance
4. To avoid modify the interface over the same two kinds of the same name as a class method call

3 Examples
1. To achieve normally hidden access to our classes are in front of a class to restrict access to modifiers, and general non-internal classes, is not allowed to have private and protected access, but the inner classes can be, so we pass inner class to hide our information. The following example can be seen
package beyondx;

public class Example {
   
    private class InsideClass implements InterfaceTest
    {
         public void test()
         {
             System.out.println("这是一个测试");
         }
    }
    public InterfaceTest getIn()
    {
        return new InsideClass();
    }
}

Above InsideClass is an internal class, access modifier is private
Client
package insidecategory;

public class TestExample {

  public static void main(String args[])
  {
    Example a=new Example();
    InterfaceTest a1=a.getIn();
    a1.test();
  }
}

From this code which I only know that Example of getIn () method can return an instance of InterfaceTest but I do not know this case is so achievable. And because InsideClass is private, so if we do not look at the code, then do not see this specific class name, so that it could achieve a good hiding.
2. Can be unconditional access to all the elements of the external class
package beyondx;

public class TagBean {

 private String name="luffyke";
   private class InTest
   {
      public InTest()
      {
          System.out.println(name);
      }
   }
   public void test()
   {
    new InTest();
   }
   public static void main(String args[])
   {
       TagBean bb=new TagBean();
       bb.test();
   }
}

See the above name of this variable is defined in the TagBean private variables inside. This variable is in the internal class can be unconditional access System.out.println (name);
3. Can achieve multiple inheritance, this feature is very important, personally believe that it is the largest within the class there is one of the reasons. It is precisely because his presence makes the Java's inheritance mechanism more perfect. We all know that Java can only inherit one class, which we do not have multiple inheritance in the class to learn prior to the internal interface is achieved. However, sometimes there are a lot of inconvenient to use interface areas. For instance, we realize that it must implement an interface on the inside all the methods. With his inner class is not the same. It can make our class inherit a number of specific classes or abstract classes. We look at the following example.
Class 1
package beyondx;

public class Example1 {

   public String name()
   {
       return "luffyke";
   }
}

Class 2
package beyondx;

public class Example2 {

    public int age()
    {
        return 25;
    }
}

Class 3
package beyondx;

public class MainExample 
{
   private class test1 extends Example1 
    {
        public String name()
        {
          return super.name();
        }
    }
    private class test2 extends Example2
    {
       public int age()
       {
         return super.age();
       }
    }
   public String name()
    {
    return new test1().name(); 
   }
   public int age()
   {
       return new test2().age();
   }
   public static void main(String args[])
   {
       MainExample mi=new MainExample();
       System.out.println("姓名:"+mi.name());
       System.out.println("年龄:"+mi.age());
   }
}

Draw attention to look at class 3, which were realized within the two classes test1, and test2, test1 class also inherits Example1, test2 inherits Example2, so that our class 3 MainExample have Example1 and Example2 on the methods and properties, also indirectly achieve multiple inheritance.
Fourth, to avoid modify the interface to be realized in two kinds of the same name with a class method calls.
We hypothetical what would happen if your class to inherit a class, but also to achieve an interface, but you find that you have inherited classes and interfaces with the same name there are two ways how to do? How do you distinguish them? ? This requires our internal class. See the following code interface
package beyondx;

public interface Incrementable
{
  void increment();
}

Class MyIncrement
package beyondx;

public class MyIncrement {

    public void increment()
    {
      System.out.println("Other increment()");
    }
    static void f(MyIncrement f)
    {
        f.increment();
    }

}

We look at some of the above plus black, two methods are the same. Looking at the following class to inherit these two classes if that is not within the class
package beyondx;

public class Callee2 extends MyIncrement implements Incrementable
{
public void increment()
      {
        //代码
       }
}

Would like to ask you increment () This method is part of coverage MyIncrement here methods? Or Incrementable way here. How can I transferred here MyIncrement method? Clearly this is not good distinction to make. Even if we are using in-house classes are a good solution to this problem. See the following code
package beyondx;

public class Callee2 extends MyIncrement
{
  private int i=0;
  private void incr()
  {
       i++;
       System.out.println(i);
  }
  private class Closure implements Incrementable
  {
      public void increment()
      {
        incr();
      }
  }
  Incrementable getCallbackReference()
  {
      return new Closure();
  }
}

We can use inner classes to implement interfaces, so as not to conflict with the outside of the class methods.