Nested class types

Usage and remark

Advantage

Disadvantage

static member classes

Use for public helper class, useful only in conjunction with its outer class. (Such as the operation types of a Calculater).

Save memory.

Class specific.

Not instance specific.

nonstatic member classes

Define an Adapter that allows an instance of outer class to be viewed as an instance of some unrelated class.(Such a simple mentations of the Map interface typically use nonstatic member classes to implement their collection views, which are returned by Map's keySet, entrySet, and values methods)

The association between a nonstatic member class instance and its enclosing instance is established when the former is created; it cannot be modified thereafter. It is possible, although rare, to establish the association manually using the expression enclosingInstance.new MemberClass(args).

Instance specific.

Adds time to its construction since initialization of the nested class for each instance.

anonymous classes

create function objects(Item 21) on the fly.

create process objects, such as Runnable, Thread, or TimerTask instances.

A third common use is within static factory methods (see the intArrayAsList method in Item 18).

Anonymous classes have enclosing instances if and only if they occur in a nonstatic context. But even if they occur in a static context, they cannot have any static members.

No name.

You can't instantiate them except at the point they're declared. You can't perform instanceof tests or do anything else that requires you to name the class.

You can't declare an anonymous class to implement multiple interfaces, or to extend a class and implement an interface at the same time.

Clients of an anonymous class can't invoke any members except those it inherits from its super type.

Because anonymous classes occur in the midst of expressions, they must be kept short— about ten lines or fewer—or readability will suffer.

local classes

A local class can be declared anywhere a local variable can be declared and obeys the same scoping rules. Local classes have attributes in common with each of the other kinds of nested classes.

// Typical use of a nonstatic member class

public class MySet<E> extends AbstractSet<E> {

... // Bulk of the class omitted

public Iterator<E> iterator() {

return new MyIterator();

}

private class MyIterator implements Iterator<E>{

...

}

}

Note

If you declare a member class that does not require access to an enclosing instance, always put the static modifier in its declaration.

Summary

If a nested class needs to be visible outside of a single method or is too long to fit comfortably inside a method, use a member class. If each instance of the member class needs a reference to its enclosing instance, make it nonstatic; otherwise, make it static. Assuming the class belongs inside a method, if you need to create instances from only one location and there is a preexisting type that characterizes the class, make it an anonymous class; otherwise, make it a local class.

最新文章

  1. php_zip 类
  2. (六)C#中判断空字符串的三种方法性能分析
  3. 关于 ArtifactTransferException: Failure to transfer
  4. 使用Twisted进行socket编程
  5. js模拟Map对象,实现key---value
  6. Android - 数据存储 -存储键值对
  7. 在IFrame中查找IFRAME中的元素的方式
  8. Java重头学
  9. JDK1.8源码分析之Comparable &amp;&amp; Comparator
  10. jQuery之end()和pushStack()
  11. spring mvc请求过程
  12. git命令之git stash 暂存临时代码
  13. 使用before_request来做权限和用户检查
  14. npm换国内淘宝镜像
  15. JAVA 对象内存结构
  16. oracle系列(一)sqlplus命令
  17. oracle 某一字段取反
  18. Html5 突破微信限制实现大文件分割上传
  19. Codeforces - 102222A - Maximum Element In A Stack - 模拟
  20. BZOJ - 4518: 征途(斜率优化,求N数划分为M区间的最小方差)

热门文章

  1. SQL Server中的事务日志管理(2/9):事务日志架构概述
  2. SQL Server里的自旋锁介绍
  3. sqlite3存储格式
  4. MVC ,Action方法传数据给视图有几种方式?--PS:tempData和Viewbag,还有ViewData之间的区别
  5. 如何快速开发树形列表和分页查询整合的WInform程序界面
  6. Google判断广告点击作弊的几种方式和数据
  7. jquery实现下拉框功能
  8. 35 Gallery – Ajax Slide
  9. Error generating Swagger server (Python Flask) from Swagger editor
  10. 关闭Eclipse中自动弹出console的功能