What is anonymous class?

Copied from http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html

Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.

Accessing Local Variables of the Enclosing Scope, and Declaring and Accessing Members of the Anonymous Class:

  • has access to the members of its enclosing class. 可以访问外部类的成员。
  • Cannot refer to the non-final local variable mapIconIndex defined in an enclosing scope. 不能访问 non-final local 变量。

匿名内部类容易因持有外部类的引用导致外部类不能被回收而造成内存泄漏,解决的办法是使用弱引用