Java inner classes

I. Introduction

In Java, a class may be defined inside another class or a method which, such a class called internal type. Internal class in a broad sense includes four general: internal class members, local inner classes, anonymous inner classes and static inner classes.


1, indirect implementation of Java multiple inheritance, independent of each inner class can inherit an interface, so even if it is for an external class inherits a parent, it is within the class will not have any effect.

2, complex logic code may be combined together, and the outside is hidden.

3, multi-threaded internal use.

Second, the members of the inner class

Members of the inner class is the most common internal class, which is defined as being located inside another class.

Access rules:

1, inner classes unrestricted authority breaks, direct access to the member variables outside of class.

2, prior to instantiate the inner class, you must first create an external class object, to access internal class property.

Directly create inner class formula is:

The external name of the object class name inside the class name = new class name .new external internal class name ();

3, the class can not directly access the external variables and methods within the class, require access through the internal class object.

public class Outer {
    private Inner inner = null;
    int num = 10;
    public Inner getInner() {
        if (inner == null)
            inner = new Inner();
        return inner;
    public class Inner {
        int num = 20;
        public void heart() {
            int num = 30;
            System.out.println("就近原则:" + num);  // 结果:30
            System.out.println("本类中;" + this.num);  // 结果:20
            System.out.println("外部类中:" + Outer.this.num);   // 结果:10

Class is the most common member of the internal one, but there is the above code is in detail the outer class, same class, and variable name problems within the present method within the class. Access rule in this case is:

1, the principle of proximity, access to the variable value of the present method.

2, if this class may be used to access this keyword.

3, if access to external class variable value (method), use as: External class name .this variable name (external .this class member method).

There are two ways to create:

        Outer outer = new Outer();
        Outer.Inner inner = Inner();  //必须通过Outer对象来创建

        Outer.Inner inner1 = outer.getInner();

Third, the partial inner class

If a class is defined within the process, that is, a partial inner class.
    “Local”: out of this method, other places can not be called a local inner class.

public class LocalCLass {
    public void localInner(){
        // 局部内部类
        class Inner{
            int num = 10;
            public void methodInner(){
                System.out.println("局部内部类:"+num);   // 结果:10
        // 在方法内部,调用局部内部类
        Inner inner = new Inner();


1, permissions on class modifiers:


External class: public / (default)
    Internal class: public / protected / (default) / private
    Local inner class: What can not write

2, local access to the local members of the class method:

    If you want to access the local class method where a local variable, it must be effective final modified.

    Jdk8 + from the start, as long as local variables will not actually change, you can omit the final keyword.

Why modified final keyword, and the life cycle of this object relationship.

1, new objects are out of the heap in memory of.
    2, local variables is the method to go along, the stack memory.
    3, after the end of the process, it will to a stack, local variables disappear.
    4, and the new target is still out of heap memory until GC recycling disappear.
    Therefore, when the local inner class is still the heap, if you use local variables again, and the method of local variables are already popping disappeared. Therefore, you must use the final modification is a constant, so that the local inner class can go constant pool copy. Another reason is, if not the final modification is a constant, later in the local variable value is modified, it will result in inconsistent data read local inner class situation.

Fourth, the anonymous inner class

Simple anonymous inner class is class does not understand the class name.

Generally in the actual development anonymous class is the most used, the use of anonymous inner classes when coding event listeners not only convenient, but also make the code easier to maintain.

So under what circumstances would generally use anonymous inner classes.

If the interface implementation class (subclass the parent class) need only only once. So in this case it can be omitted to achieve this class directly using anonymous inner classes.

Consider the following code, to define a first interface.

public interface IAnonymous {
    void anonymousMethod();
        // 匿名内部类的创建方式
        // 第一种方法
        IAnonymous anonymous = new IAnonymous() {
            public void anonymousMethod() {
        // 第二种方式,匿名对象
        new IAnonymous(){
            public void anonymousMethod() {

Anonymous inner classes Note:
    1, anonymous inner classes when you create objects, can only be used only once, if you want to create the same object multiple times,
    And content is the same, it must be defined separately implementation class or subclass.
    2, anonymous objects, when calling the method can only be called only once. If the same object to invoke multiple methods,
    We need to define the object name to the object.
    3, is omitted anonymous inner classes implementation class or subclass, while the object is omitted anonymous object name. The two are not the same thing.

Fifth, static inner classes

Static inner class is defined in another class inside the class, just in front of the class of more than one keyword static. Static inner class need not rely on external class, the static class member properties and this is somewhat similar, and it can not use non-static member variables or external class method, this is well understood, because no external object class under the circumstances, you can create objects inside a static class, if allowed access to non-static members of the outer class is a contradiction, because the non-static member outside the class must be attached to specific objects.

public class StaticInnerClass {
    // 外部类静态成员属性
    private static int num  = 10;

    public static class staticInner{
        public void staticMethod(){
class StaticMain{
    public static void main(String[] args) {
        StaticInnerClass.staticInner innerClass = new StaticInnerClass.staticInner();

VI Summary

Each class has its own internal usage scenarios, so for the use of inner classes should be selected according to their business needs. In particular, should pay attention to the details of the use of inner classes, and the use of rules.

The last self-study summarized above are, if not also please leave a message advice.

Thanks for reading!

Leave a Reply