`

Java enums in JDK 1.4

    博客分类:
  • JSE
阅读更多
Constantly, we need to define enums. In Java 1.5, there is also a keyword. However, in JDK 1.4, we are out of the luck and have to come up something to simulate enums.

The motivation for enums is to avoid hardcoding same constants everywhere because it makes maintanence a nightmare, hard to track where the same constants are used, whether the upper/lower cases mean the same, the change of any constant requires changes in multiple places.

A simple way to create enums is using the infamous "public static final int". However, the general type without restriction(like int type) makes the interface signature(and consequently IDE prompts) quite confusing and error prone(pass in invalid data). So a better solution is proposed in "Effective Java" #21. Though we are strong typed, this approach creates a new problem - == is broken because we can't maintain the singletons in the vast different environments.

The reasons are:

1. there are clusters, acrossing several JVMs.
2. cross wires to a different JVM
3. different classloaders(so overriding readResolve() doesn't work either).

The compromise is to use equals().

To make matters worse, if there is a data structure among these constants, e.g., a tree structure, meaning, the constants are not quite mutual exclusive, then we need to maintain the structure as well. A typical case is the object class types in a class hierachy. For example, suppose we have a top class Automobile, and subclasses Car, Truck, Utility/Sports, and each of these has subclasses corresponding to manufactures/brand, BMW, BENZ, Toyota, etc. Then for each manufacture, they have different models. So we could keep going and eventually get a class hierarchy.

Now for some reason(many reasons, like performance, grouping, and others), we need to map this class hierarchy to an enum hierarchy and use it. A typical naive approach is using switch or if-else, a better approach is to build a visitor around it with double dispatch. However, building a fast visitor on top of non-exclusive enum hierarchy is not as trivial as we thought.
Another twist is documented in Hardcore Java chapter 7, when we have an enum is one class, and have another enum in its subclasses. So the challenge now becomes how to build an universal enum structure that could spread in several classes and is non-exclusive, while keep out the if-else checks.


References:

Hardcore Java, chapter 7
Javaworld java tip 122
分享到:
评论

相关推荐

    java-enums

    java-enums

    Java.Crash.Course.1541019245.epub

    You will understand how to set a simple operator in Java You will learn all the technical Java programming language such as Loops and Arrays, Boolean Logic, Methods, Inheritance and Polymorphism, ...

    Java.7.A.Comprehensive.Tutorial

    Designed as a guidebook for those who want to become a Java developer, Java 7: A Comprehensive Tutorial discusses the essential Java programming topics that you need to master in order teach other ...

    JAVA白皮书(英文版)

    6.4Security in the Java Networking Package 6.5Summary 7. Multithreading 7.1Threads at the Java Language Level 7.2Integrated Thread Synchronization 7.3Multithreading Support--...

    Code_java_cardo5v_enums_

    在开发java程序时,经常涉及到字典表的情况,很多同学喜欢建一些数据表来维护。其实你可以试试定义枚举项实现1.定义枚举项参考代码:SpecialStatusEnum.java2.定义辅助类参考代码:EnumUtil.java3.使用参考参考代码...

    Ruby-TranslateEnum简单零依赖Rails的Enums翻译gem

    TranslateEnum:简单,零依赖Rails的Enums翻译gem

    java代码生成器(可以生成service,controller,enums等)

    除了生成entity,example,mapper外,额外生成controller,service,impl,enums,extMapper等。注意:生成之后修改配置,以防被覆盖。如果生成过程有问题,联系QQ:16849092。解压之后配置 config ,按说明配置参数即可。

    JAVA高级编程中Enum枚举

    java高级编程 Enum枚举 包括枚举的例子,问题 及一些文档,是复习的好资源

    Pro.Java.8.Programming.3rd.Edition.1484206428

    You'll discover the particulars of working with the Java language and APIs to develop applications in many different contexts. You will also delve into more advanced topics like lambda expressions, ...

    Java Crash Course [2016]

    Java Crash Course - The Complete Beginner's Course to Learn Java Programming in 21 Clear-Cut Lessons Paperback – December 20, 2016 Are you bored of the traditional methods people tell you to use to ...

    Java Language Specification, Third Edition

    It provides full coverage of all new features added in since the previous edition including generics, annotations, asserts, autoboxing, enums, for each loops, variable arity methods and static import...

    Pro.Java.8.Programming.3rd.Edition.1484206428.epub

    You'll discover the particulars of working with the Java language and APIs to develop applications in many different contexts. You will also delve into more advanced topics like lambda expressions, ...

    Pro Java 8 Programming(Apress,3ed,2015)

    You'll discover the particulars of working with the Java language and APIs to develop applications in many different contexts. You will also delve into more advanced topics like lambda expressions, ...

    Beginning Java 9 Fundamentals(Apress,2ed,2017)

    Write your first Java programs with an emphasis on learning object-oriented programming in Java Work with data types, operators, statements, classes and objects Handle exceptions, assertions, strings ...

    enums_COD_

    Enums IDa for COD Warfare

    Learn.Java.for.Android.Development_Apress.2010+src

    Tools: You will learn how to use the JDK''s javac (compiler), java (application launcher), javadoc (Java documentation generator), and jar (Java archive creator, updater, and extractor) tools....

    Addison.Wesley.The.Java.Programming.Language.4th.Edition.Aug.2005.chm

    It also covers most of the classes in the main packages (java.lang, java.util, java.io) as implemented in the J2SE™ Development Kit 5.0 (more commonly known as JDK 5.0, or in the older nomenclature ...

    Java语言规范(第三版) 英文

    It provides full coverage of all new features added since the previous edition, including generics, annotations, asserts, autoboxing, enums, for-each loops, variable arity methods, and static import ...

    Java-Language-Specification-3rd-Edition

    全面、准确、详尽地介绍了Java语言及其语法,论述了Java编译器所要检查的语法和Java运行模式的各个方面,同时还描述了Java语盲最重要的新特征(包括generics、annotationS、aSSertS、autoboxing、enumS、for-...

    advanced java.pdf

    5、How and when to use Enums and Annotations 6、How to write methods efficiently 7、General programming guidelines 8、How and when to use Exceptions 9、Concurrency best practices 10、Built-in ...

Global site tag (gtag.js) - Google Analytics