Principle

Never derive a value associated with an enum from its ordinal; store it in an instance field instead.

Bad practice Demo

// Abuse of ordinal to derive an associated value - DON'T DO THIS

public enum Ensemble {

SOLO, DUET, TRIO, QUARTET, QUINTET,

SEXTET, SEPTET, OCTET, NONET, DECTET;

public int numberOfMusicians() {return ordinal() + 1;}

}

// The right way

public enum Ensemble {

SOLO(1), DUET(2), TRIO(3), QUARTET(4), QUINTET(5),

SEXTET(6), SEPTET(7), OCTET(8), DOUBLE_QUARTET(8),

NONET(9), DECTET(10), TRIPLE_QUARTET(12);

private final int numberOfMusicians;

Ensemble(int size) { this.numberOfMusicians = size; }

public int numberOfMusicians() { return numberOfMusicians; }

}

Disadvantages

  1. If the constants are reordered, the numberOfMusicians method will break.
  2. If you want to add a second enum constant associated with an int value that you've already used, you're out of luck.
  3. You can't add a constant for an int value without adding constants for all intervening int values.

Summary

The Enum specification has this to say about ordinal: "Most programmers will have no use for this method. It is designed for use by general-purpose enum based data structures such as EnumSet and EnumMap." Unless you are writing such a data structure, you are best off avoiding the ordinal method entirely.

Effective Java 31 Use instance fields instead of ordinals的更多相关文章

  1. Effective Java 77 For instance control, prefer enum types to readResolve

    The readResolve feature allows you to substitute another instance for the one created by readObject ...

  2. effective java——31用实例域代替序数

    1,永远不要根据枚举的序数导出与它关联的值,而是要将他保存在一个实例域中.(ordinal()) public enum Ensemble { SOLO, DUET, TRIO, QUARTET, Q ...

  3. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  4. 《Effective Java》读书笔记 - 6.枚举和注解

    Chapter 6 Enums and Annotations Item 30: Use enums instead of int constants Enum类型无非也是个普通的class,所以你可 ...

  5. Effective Java 目录

    <Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...

  6. 【Effective Java】阅读

    Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...

  7. Effective Java 第三版——31.使用限定通配符来增加API的灵活性

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  8. Effective Java —— 谨慎覆盖clone

    本文参考 本篇文章参考自<Effective Java>第三版第十三条"Always override toString",在<阿里巴巴Java开发手册>中 ...

  9. Effective Java 创建和销毁对象

    <Effective Java>阅读笔记,用适合自己理解的方式提炼该书内容.<Effective Java>是一本很实用的书,阅读方法应该是快速的领会,总结,然后应用.而非,一 ...

随机推荐

  1. [Solution] 一步一步WCF(2) 终结点Endpoint

    繁忙的一天又一天,不管其他,先继续WCF吧. Endpoint包含地址,绑定,契约三要素.WCF作为一个Windows平台下最大的通信框架.通过终结点承载了所有通信功能.所以终结点的作用将非常重要. ...

  2. 餐厅到店点餐系统(APP)

    MY-HR 成员: 角色分配 学号 博客园 丘惠敏 PM项目经理 201406114203 http://www.cnblogs.com/qiuhuimin/ 郭明茵 用户 201406114204 ...

  3. MySQL字符串转日期类型

    MySQL字符串转日期类型 select str_to_date('2014-08-20 00:00:00', '%Y-%m-%d %H:%i:%s'); >2014-08-20 00:00:0 ...

  4. HTTPS 概述

    [[  From   https与http的区别   ]] 什么是HTTPS HTTPS(Secure Hypertext Transfer Protocol)安全超文本传输协议 它是一个安全通信通道 ...

  5. 微软开源资源 NET Foundation Projects

    网络基础设施.在管理项目(管理工作)的网络基础.目前主要包括网络平台上编译通过.(“罗斯林”)以及项目的ASP.NET的家庭,都是开放的微软开放的技术来源,Inc.(MS开放技术).Xamarin贡献 ...

  6. cppcheck

    http://sourceforge.net/projects/cppcheck/files/?source=navbar https://github.com/danmar/cppcheck htt ...

  7. JPA学习(2)注解

    上一篇学习了JPA的helloworld,也初略的使用了一些注解,接下来就细细的了解一下有哪些注解,和这些注解的作用 JPA的基本注解: ①@Entity,@Table,@Id,@GeneratedV ...

  8. Html 网页布局(一)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...

  9. 股票价格涨跌预测—基于KNN分类器

    code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...

  10. C++之内联函数与constexpr

    inline 函数 规模小,流程直接且频繁调用 cout<<shortString(s1,s2)<<endl; = cout<<(s1.size()<s2.s ...