zoukankan      html  css  js  c++  java
  • 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/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    springboot入门系列(一):简单搭建springboot项目
    springboot入门系列(二):SpringBoot整合Swagger
    springboot入门系列(三):SpringBoot教程之RabbitMQ示例
    springboot入门系列(四):SpringBoot和Mybatis配置多数据源连接多个数据库
    Linux下安装RabbitMQ
    Mybatis原理之数据源和连接池
    springboot入门系列(五):SpringBoot连接多RabbitMQ源
    jsp中<c:foreach>分页标签的序号问题
    Java中删除一个文件夹下的所有文件(包括子目录内的文件)
    接口的幂等性
  • 原文地址:https://www.cnblogs.com/haokaibo/p/favor-static-member-classes-over-nonstatic.html
Copyright © 2011-2022 走看看