Effetive Java 22 Favor static member classes over nonstatic
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.
Effetive Java 22 Favor static member classes over nonstatic的更多相关文章
- (转)Java中的static关键字解析
转自http://www.cnblogs.com/dolphin0520/p/3799052.html 一.static关键字的用途 在<Java编程思想>P86页有这样一段话: “sta ...
- java中的static详解
如果一个类成员被声明为static,它就能够在类的任何对象创建之前被访问,而不必引用任何对象.static 成员的最常见的例子是main( ) .因为在程序开始执行时必须调用main() ,所以它被声 ...
- Java中的static关键字解析 转载
原文链接:http://www.cnblogs.com/dolphin0520/p/3799052.html Java中的static关键字解析 static关键字是很多朋友在编写代码和阅读代码时碰到 ...
- Java中的static关键字解析(转自海子)__为什么main方法必须是static的,因为程序在执行main方法的时候没有创建任何对象,因此只有通过类名来访问。
Java中的static关键字解析 static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键 ...
- java 项目得到jar和classes路径
java 项目得到jar和classes路径 public static String getJarPath(Class clazz) { String path = clazz.getProtect ...
- 【搬运工】——Java中的static关键字解析(转)
原文链接:http://www.cnblogs.com/dolphin0520/p/3799052.html static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大 ...
- c++ 静态类成员函数(static member function) vs 名字空间 (namespace)
好多人喜欢把工具函数做成static member function.这样以增加隐蔽性和封装性,由其是从C#,java转而使用c++的开发人员. 例如: class my_math { public: ...
- Java 静态对象 static
什么是静态变量 大家都知道,我们可以基于一个类创建多个该类的对象,每个对象都拥有自己的成员,互相独立. 然而在某些时候,我们更希望该类所有的对象共享同一个成员.此时就是 static 大显身手的时候了 ...
- (转)Java中的static关键字解析
转载: http://www.cnblogs.com/dolphin0520/p/3799052.html 一.static关键字的用途 在<Java编程思想>P86页有这样一段话: &q ...
随机推荐
- 查找表或其他对象在某个Server上的存在
EXEC sp_MSforeachdb 'use ? ; IF EXISTS(SELECT top 1 1 FROM sys.syscomments WHERE text LIKE ''%test% ...
- Gradle学习系列之三——读懂Gradle语法
在本系列的上篇文章中,我们讲到了创建Task的多种方法,在本篇文章中,我们将学习如何读懂Gradle. 请通过以下方式下载本系列文章的Github示例代码: git clone https://git ...
- C++ - 多线程的实现
支持多线程可谓是C++语言最大的变化之一. 此前,C++只能利用操作系统的功能(Unix族系统使用pthreads库),或是例如OpenMP和MPI这些代码库,来实现多核计算的目标. C++本身并没有 ...
- vs.net_2003 下载 虽然是老古董了,但还是很有用的。
系统要求 支持的操作系统: Windows 2000; Windows NT; Windows Server 2003; Windows XP 以下VS2003的下载链接: http://bcsoft ...
- 【分享】深入浅出WPF全系列教程及源代码
来源:http://blog.csdn.net/yishuaijun/article/details/21345893 本来想一篇一篇复制的.但是考虑到别人已经做过了,就没有必要了吧,就给大家一个目录 ...
- (旧)子数涵数·PS——换脸
一.打开Photoshop,打开下载好的两张素材图(均在百度下载的). 二.选中人物脸的部分,不需要很精确(哪种抠图方式都行,我用的是矩形选框工具),再使用移动工具(快捷键为V,或者按着Ctrl),将 ...
- percona 5.6升级到5.7相关error及解决方法
今早,把开发环境的mysql升级到了5.7.15,5.6数据导入后,启动一切正常,检查.err日志,发现有如下异常: 2016-10-31T00:29:33.187073Z 0 [Warning] S ...
- Telegram传奇:俄罗斯富豪、黑客高手、极权和阴谋…
说了很久要写Telegram的故事,一直拖延没有写.在我拖延的这段时间里面,Telegarm继续快速增长,前几天,在旧金山的TechCrunch Disrupt活动上,创始人Durov说现在Teleg ...
- Orchard源码:Logging
试着用markdown写些东西.貌似博客园支持的还有问题,代码片段显示错位,还得另外上传图片.还是用普通方法写写随笔好了. Logging相对也是比较松耦合的模块,可以随时提取出来用在自己的项目中.其 ...
- innerHtml and Jquery.html()
1. innerHtml是Dom HTML的属性 是只读的,不能写入. 2. JQuery只能调用.html(),它可以加参数,改变原HTML内容. http://api.jquery.com/htm ...