• 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.

       

    作者:小郝
    出处:http://www.cnblogs.com/haokaibo/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    spring AOP (使用AspectJ的注解方式 的aop实现) (6)
    spring aop的前奏,动态代理 (5)
    Python基础笔记系列九:变量、自定义函数以及局部变量和全局变量
    Python基础笔记系列八:字符串的运算和相关函数
    Python基础笔记系列六:字典
    Python基础笔记系列五:元组
    Python基础笔记系列四:工具的安装与配置
    Python基础笔记系列三:list列表
    Python基础笔记系列二:分支和循环
    Python基础笔记系列一:基本工具与表达式
  • 原文地址:https://www.cnblogs.com/haokaibo/p/favor-static-member-classes-over-nonstatic.html
Copyright © 2020-2023  润新知