Effective Java 31 Use instance fields instead of ordinals
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
- If the constants are reordered, the numberOfMusicians method will break.
- If you want to add a second enum constant associated with an int value that you've already used, you're out of luck.
- 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的更多相关文章
- 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 ...
- effective java——31用实例域代替序数
1,永远不要根据枚举的序数导出与它关联的值,而是要将他保存在一个实例域中.(ordinal()) public enum Ensemble { SOLO, DUET, TRIO, QUARTET, Q ...
- 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 ...
- 《Effective Java》读书笔记 - 6.枚举和注解
Chapter 6 Enums and Annotations Item 30: Use enums instead of int constants Enum类型无非也是个普通的class,所以你可 ...
- Effective Java 目录
<Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...
- 【Effective Java】阅读
Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...
- Effective Java 第三版——31.使用限定通配符来增加API的灵活性
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- Effective Java —— 谨慎覆盖clone
本文参考 本篇文章参考自<Effective Java>第三版第十三条"Always override toString",在<阿里巴巴Java开发手册>中 ...
- Effective Java 创建和销毁对象
<Effective Java>阅读笔记,用适合自己理解的方式提炼该书内容.<Effective Java>是一本很实用的书,阅读方法应该是快速的领会,总结,然后应用.而非,一 ...
随机推荐
- [linux]删除目录下的一类文件
find 目录 -name "*.类型" | xargs rm -f 通过find命令,查找指定目录下的某一类型的文件.并通过管道传递给xargs,执行后面的rm -f命令. 最终 ...
- 点餐系统mealsystem.sql
/* Navicat MySQL Data Transfer Source Server : localhost Source Server Version : 50162 Source Host : ...
- 适用于jquery1.11.1的ajaxfileupload.js
ajaxfileupload源码 解决上传成功不走success的问题 解决高版本jquery兼容性问题 jQuery.extend({ createUploadIframe: function(id ...
- MVC学习笔记索引帖
[MVC学习笔记]1.项目结构搭建及单个类在各个层次中的实现 [MVC学习笔记]2.使用T4模板生成其他类的具体实现 [MVC学习笔记]3.使用Spring.Net应用IOC(依赖倒置) [MVC学习 ...
- web服务器之nginx与apache
最近准备架设php的web服务器,以下内容可供参考. 1.nginx相对于apache的优点: 轻量级,同样起web 服务,比apache占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞 ...
- C#的注释和快速开启工具的命令
1.注释的方法 1)sqlserver中,单行注释:—— 多行注释:/****/ 2)C#中,单行注释:// 多行注释:/****/ 3)C#中多行注释的快捷方式:启用ctrl+E+C ,撤 ...
- 将表数据生成Insert脚本
set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgo-- =============================================-- Author ...
- 模拟---LCR
HDU 2778 Description LCR is a simple game for three or more players. Each player starts with three ...
- java int转byte和long转byte
在网络编程中,出于节约带宽或者编码的需要,通常需要以原生方式处理long和int,而不是转换为string. public class ByteOrderUtils { public static b ...
- 清除浮动after
.clearf{display: inline-block;} .clearf:after { content: "."; display: block; height:; cle ...