About java inner classes

This article is very well written about java inner classes. Respect the author's labor results:
http://www.cnblogs.com/dolphin0520/p/3811445.html

Now I will write my own summary based on the author:
   Here is a question, whether the interface can be new, the general answer Neither is possible. But in fact it is possible, when? When he acts as an anonymous inner class.

   The main purpose of inner classes is anonymous inner classes, which are used for some callbacks and some event calls.
  
   Inner class accesses local variables of outer class Why local variables must be final? Because java uses the copy method to copy the value of the local variable, so if the variable is modified in the inner class, the local variable will be inconsistent with the variable in the inner class. In order to solve this problem, the final keyword is added to not allow the variable to modify.

A singleton pattern based on java inner class:
public class SingletonInner {
private SingletonInner(){

}
private static class Inner{
private final static SingletonInner singletonInner = new SingletonInner();
}
public static SingletonInner getInstance(){
return Inner.singletonInner;
}


}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326949132&siteId=291194637