Conclusion:

  • A Java closure is a callable object that records information from the scope in which it was created. An entity object composed of functions and reference environments.
  • When the parent class and the interface to be implemented have the same method name. The solution is to use closures and inner classes to implement interfaces. You can also implement multiple inheritance with inner classes. Static inner classes implement the secure singleton pattern.
  • Closures cause resources not to be recycled.

1.1 Free variables

Before you can learn the concept of closures, you need to understand the concept of free variables. Free variable: a variable not declared in the current scope. (that is, variables declared in other scopes)

Public class Demo1 {private int x = 0; void f1(){ int res = 1 + x; System.out.println(x); }}Copy the code

The x used by res in the above code is not defined in f1(), where x is a free variable.

1.2 Java inner Classes

1.2.1 Definition of inner classes

Java inner classes define another class inside a class. The inner class references the variables of the outer class through this. And inner classes need to be attached to outer classes to exist. The inner class object is created using.new of the outer class object.

public class Outer { private int y = 5; private class Inner { private int x = 10; public int add() { return x + y; } public Outer getOuter(){ return Outer.this; }}}Copy the code

Here, the add method in the Inner class Inner holds the free variable X of the Inner class’s scope, forming a closure.

1.2.2 The role of inner classes

The biggest function of inner class is that each inner class can inherit a parent class, realizing multiple inheritance of classes.

public class TestClass extends AbstractFather { @Override public String sayHello() { return fatherName; } class TestInnerClass extends AbstractMother { @Override public String sayHello() { return motherName; }}}Copy the code

AbstractFathor and AbstractMother both contain the sayHello method.

1.2.3 Classification of inner classes

  1. Local inner class: defined inside a method, or something like if… Code block.
public class Parcel5 {
public Destionation destionation(String str){
    class PDestionation implements Destionation{
        private String label;
        private PDestionation(String whereTo){
            label = whereTo;
        }
        public String readLabel(){
            return label;
        }
    }
    return new PDestionation(str);
}
Copy the code
  1. Static inner classes: A static inner class is a static inner class. The main difference between a static inner class and a normal member inner class is that the static inner class has no reference to the enclosing class. Therefore, its creation does not depend on the enclosing class, but it cannot use any non-static member variables and methods of the enclosing class.
public class Singleton { private static class SingletonHolder { private static final Singleton INSTANCE = new Singleton(); } private Singleton (){} public static final Singleton getInstance() { return SingletonHolder.INSTANCE; }}Copy the code

The biggest use of static classes is to create thread-safe singleton patterns.

  1. Anonymous inner classes: Anonymous inner classes are unnamed inner classes that are often used when we need to quickly create multiple threads:
new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("hello");
        }
    }).start();
Copy the code

Anonymous inner classes have no access modifiers and no constructors. An anonymous inner class exists attached to an interface and cannot be created if the implemented interface does not exist. If an anonymous inner class accesses a free variable of its method, that free variable is final.

1.3 Java closures

Closures consist of a function + reference environment. True closures move the variable referenced by a function into heap memory when the variable is on the stack and assign the reference to it. Because Java is value passing and does not support reference passing, a Java closure counts as a half-closure. Java handles referenced free variables by copying the value into a function.