The definition of a class can be placed inside the definition of another class. This is called an inner class. In Java, inner classes are divided into four categories:

  1. Member inner class
  2. Method inner class
  3. Anonymous inner class
  4. Static inner class

Member inner class

Member inner classes are the most common inner classes:

public class OutClass {
    private String out;
    public String name = "out";

    public InnerClass getInner() {return new InnerClass();
    }
    public class InnerClass {
        private String inner;
        public String name = "inner";

        public String getOutName() {// Access external class variablesreturn OutClass.this.name;
        }

        public String getInnerName() {

            returnname; }}}Copy the code

InnerClass is an InnerClass that, like a member of the outer class OutClass, has unlimited access to member variables and methods of the outer class. Note, however, that when a member’s inner class has a member variable or method with the same name as the outer class (for example, name in code), when the variable is accessed through the inner class, the default access is to the member of the inner class. If you need to access variables of an external class, you need: outclass.this.name. If an external class wants to access member variables and functions of an inner class, it must first create an object of the inner class and then access it through a reference to that object. To create an inner class, an object of the outer class must exist:

OutClass.InnerClass innerClass1 = outClass.getInner();
OutClass.InnerClass innerClass2 = outClass.new InnerClass();
Copy the code

As with member variables, inner classes can have private access, protected access, and public decorations.

Method inner class

A method or a class in a scope is a method inner class.

private Object fun() {
        class Fun {

        }
        return new Fun();
    }
Copy the code

Its access is restricted to the method and it cannot have public, protected, private, or static modifiers.

Anonymous inner class

As Android developers, the most common inner class is the anonymous inner class, and yes, when writing code for event listening:

view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               
        });
Copy the code

Most anonymous inner classes are used for interface callbacks, and anonymous inner classes cannot have access and static modifiers. Anonymous inner classes are used to inherit from other classes or implement interfaces. No additional methods need to be added, just implementations or overrides of inherited methods.

Static inner class

A static inner class is also a class defined in another class. If a member inner class is understood as a member variable, then a static inner class can be understood as a static member variable. A static inner class cannot be a nonstatic member variable or method of an external class.

public class OutClass {
    private String out;
    public String name = "out";

    public InnerClass getInner() {return new InnerClass();
    }
    static class InnerClass {
        private String inner;
        public String name = "inner"; InnerClass = new outClass.innerClass ();Copy the code

role

The functions of inner classes are summarized as follows:

  1. Inner class methods can access the data in the scope in which the class is defined, including private data that is modified by private. They can organize classes that have certain logical relationships together, and they can be hidden from the outside world
  2. Each inner class can independently inherit an implementation of the interface, which can overcome the shortcomings of Java single inheritance and complete multi-inheritance solutions
  3. It is convenient to write event drivers and thread code. When we want to define a callback function but don’t want to write a lot of code, we can choose to use anonymous inner classes

Note that both non-static inner classes and anonymous inner classes hold references to outer classes.

Related articles recommend inner classes in Kotlin