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的更多相关文章

  1. (转)Java中的static关键字解析

    转自http://www.cnblogs.com/dolphin0520/p/3799052.html 一.static关键字的用途 在<Java编程思想>P86页有这样一段话: “sta ...

  2. java中的static详解

    如果一个类成员被声明为static,它就能够在类的任何对象创建之前被访问,而不必引用任何对象.static 成员的最常见的例子是main( ) .因为在程序开始执行时必须调用main() ,所以它被声 ...

  3. Java中的static关键字解析 转载

    原文链接:http://www.cnblogs.com/dolphin0520/p/3799052.html Java中的static关键字解析 static关键字是很多朋友在编写代码和阅读代码时碰到 ...

  4. Java中的static关键字解析(转自海子)__为什么main方法必须是static的,因为程序在执行main方法的时候没有创建任何对象,因此只有通过类名来访问。

    Java中的static关键字解析 static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键 ...

  5. java 项目得到jar和classes路径

    java 项目得到jar和classes路径 public static String getJarPath(Class clazz) { String path = clazz.getProtect ...

  6. 【搬运工】——Java中的static关键字解析(转)

    原文链接:http://www.cnblogs.com/dolphin0520/p/3799052.html static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大 ...

  7. c++ 静态类成员函数(static member function) vs 名字空间 (namespace)

    好多人喜欢把工具函数做成static member function.这样以增加隐蔽性和封装性,由其是从C#,java转而使用c++的开发人员. 例如: class my_math { public: ...

  8. Java 静态对象 static

    什么是静态变量 大家都知道,我们可以基于一个类创建多个该类的对象,每个对象都拥有自己的成员,互相独立. 然而在某些时候,我们更希望该类所有的对象共享同一个成员.此时就是 static 大显身手的时候了 ...

  9. (转)Java中的static关键字解析

    转载: http://www.cnblogs.com/dolphin0520/p/3799052.html 一.static关键字的用途 在<Java编程思想>P86页有这样一段话: &q ...

随机推荐

  1. Direct3D11学习:(二)基本绘图概念和基本类型

    转载请注明出处:http://www.cnblogs.com/Ray1024   一.概述 在正式开始学习D3D11之前,我们必需首先学习必要的基础知识. 在这篇文章中,我们将介绍一下Direct3D ...

  2. [New Portal]Windows Azure Virtual Machine (10) 自定义Windows Azure Virtual Machine模板

    <Windows Azure Platform 系列文章目录> 通过之前的文章,我相信大家对微软Windows Azure Virtual Machine有一定的了解了. 虽然微软提供了非 ...

  3. python面向对象编程(下)

    本篇详细介绍了Python 中类的成员.成员修饰符.类的特殊成员以及两个综合运用实例. 环境为:python3.5.1 类的成员 类的成员包括三大类:字段.方法和property属性 注:关于这三类成 ...

  4. Win10 + VS2015 下编译 Qt5.6.0

    近期由于另外一个项目需要用 vs2015 ,于是把 Qt 也升级到了 5.6.0 .由于项目需要兼容 Windows xp ,所以需要自己编译 Qt .这里大致整理一下编译的过程. 安装 Active ...

  5. Eclipse魔法堂:任务管理器

    一.前言        Eclipse的任务管理器为我们提供一个方便的入口查看工程代办事宜,并定位到对应的代码行继续之前的工作. 二.使用示例        示例1: /** * @Descripti ...

  6. 一个关于explain出来为all的说明及优化

    explain sql语句一个语句,得到如下结果,为什么已经创建了t_bill_invests.bid_id的索引,但却没有显示using index,而是显示all扫描方式呢,原来这还与select ...

  7. [Perl]抓取个人的所有闪存+格式化保存为文本

    以下代码保存为utf8文本格式 环境:ActivePerl v5.16 built for MSWin32-x86 两个要调整的地方: for my $i (17..45) {  这里改成自己对应的页 ...

  8. C#中得到两个数百分比 (转)

    //此方法得到的百分比后小数太多,不行double percent=Convert.ToDouble(2)/Convert.ToDouble(34); string result=(percent*1 ...

  9. JQ关于浏览器宽高的获取方式

    JQ关于浏览器宽高的获取方式 alert($(window).height()); //浏览器时下窗口可视区域高度alert($(document).height()); //浏览器时下窗口文档的高度 ...

  10. 不可或缺 Windows Native (9) - C 语言: 动态分配内存,链表,位域

    [源码下载] 不可或缺 Windows Native (9) - C 语言: 动态分配内存,链表,位域 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 动态分配内存 链 ...