I prefer that all of my Java classes provide for immutable objects unless there is a compelling need for them to be mutable. I'm not the only one who feels this way. In Effective Java (Item 15 ["Minimize Mutability"] in the Second Edition and Item 13 ["Favor Immutability"] in the First Edition), Joshua Bloch extols the virtues of immutable objects and emphasizes in bold text:
Classes should be immutable unless there's a very good reason to make them mutable.
Another proponent of immutable objects is Martin Odersky of Scala fame. He has stated about immutable objects:
we recommend that if you can make [objects] immutable, that's good, and you should try to find out where you can make objects immutable.
Conceptually, it sounds easy to create immutable objects. However, there are a couple nuances to note when ensuring that a Java class is defined so that its instantiated objects are immutable. Several resources outline the steps necessary to write a Java class that will lead to immutable Java objects. These resources include the previously mentioned Joshua Bloch Effective Java items on immutable objects. Other resources outlining the steps for making a Java object immutable include Java Practices, the Java Tutorials, JavaWorld's article Mutable or Immutable?, and developerWorks's To Mutate or Not to Mutate?.
Java classes that support immutable objects have the following characteristics:
• No "set" or mutator methods (object's state is set at instantiation/construction and cannot be changed later).
• Implementation inheritance explicitly disallowed.
• Fields are final and private.
• Its data members are either immutable or only provides access to defensive copies (also covered in Effective Java) of its mutable members.
As threading becomes more significant, immutable objects become more important. Trendy languages such as Scala and Clojure emphasize the importance of immutable data structures and immutable objects in their very design.
I have found that it works best for me to start with my objects as immutable objects and then only relax the immutability as absolutely necessary. This minimizes the mutability when it is required. In general, I resist the urge to "easily" use the IDE feature that quickly and thoughtlessly creates getter and setter methods for my new classes. I have found that it is better to think about how the class is used and only provide the mutability that is absolutely necessary.
相关推荐
### Immutable Objects in Java 在Java编程语言中,不可变对象(Immutable Objects)是一个重要的概念,尤其是在构建健壮、易于维护的应用程序时。本篇将基于提供的文件内容来深入探讨不可变对象的概念及其在Java中...
本知识点将深入探讨Java多线程设计以及如何利用“不可变对象”(immutable objects)来避免多线程环境中的非安全问题。 一、Java多线程基础 1. 线程的创建:Java提供了两种创建线程的方式——继承Thread类和实现...
4. **可变与不可变对象(Mutable vs Immutable Objects)**: 通过示例代码展示了如何创建不可变对象,以及不可变对象的益处和实现策略。 5. **泛型(Generics)**: 书中深入讲解了Java泛型的用法,包括类型擦除、...
- 不可变对象(Immutable Objects):一旦创建就不能被修改的对象,天然线程安全。 学习Java并发编程,重点在于理解线程如何协同工作,以及如何确保并发执行的正确性和高效性。通过深入掌握Java并发API,可以为大型...
Real-World Objects to UML Diagrams and Java 9 via JShell Classes and Instances Encapsulation of Data Mutable and Immutable Classes Inheritance, Abstraction, Extension, and Specialization Members ...
在Java编程中,不可变对象(Immutable Objects)是一种一旦创建后就不能被修改的对象。这种特性在多线程环境中尤其重要,因为不可变对象是线程安全的,它们不会因为并发修改而引发数据不一致的问题。Guava库是Google...
- 对于不可变对象(immutable objects)的错误使用,比如String类的错误使用方式。在Java中,String对象是不可变的,一旦创建就无法更改。文档中展示了错误地对String对象进行操作的实例,这是由于对Java中引用类型...
在并发编程中,不变对象(immutable objects)扮演着重要角色,因为它们天然具备线程安全性。JSR 133的改变确保了,一旦对象的final字段在构造函数中被初始化,其他线程将始终能看到这些初始化的值。这提高了不变...
### 不可变对象(Immutable Objects) 在并发编程中,不可变对象是不可更改的对象,一旦创建了它的实例,就不能修改它的状态。不可变对象可以提供线程安全保证,因为它们的状态不会在多个线程间改变。 ### 高级...
11. 在 Java 中不可变对象(Immutable Objects): - 一旦被创建,状态不能改变。 - 优点是线程安全。 - 示例:String、Integer。 12. Java 中的 switch 语句: - 在 JDK 1.7 之后支持 String 类型参数。 13. ...
1. **We Are All Objects** - **对象导向**: 无论是C#还是Java,都是一种面向对象的语言,这意味着所有的代码都被组织为类和对象。 - **一切皆对象**: 在这两种语言中,所有事物都是对象,包括基本数据类型通过...
8. **可变与不可变对象(Mutable vs Immutable Objects)**:书中探讨了如何创建和使用不可变对象,以及它们在并发编程中的优势。 9. **多线程编程(Multithreaded Programming)**:Java提供了丰富的并发工具,如`...
不可变字段与对象(Final fields & Immutable objects) 不可变字段(final fields)在对象创建后必须被赋予一个值,此后其值不可更改。不可变字段的设定值对所有线程可见,即使在无同步的情况下也是如此。不可变...
8. **不可变对象 (Immutable Objects)**: - **描述**:不可变对象是指一旦创建就不能被改变的对象。在Java中,可以通过将对象的所有字段声明为`final`来创建不可变对象。不可变对象是线程安全的,因为它们的状态...
- 对于不变对象(immutable objects),所有域都是final和静态的,这样可以提高性能并防止意外修改。 总之,理解和熟练运用Java的静态域和实例域以及它们与构造方法的关系是Java程序员的基本技能。通过合理的设计和...
2 對象與相等性(Objects and Equality) 25 實踐8:區分reference type 和primitive type 25 實踐9:區分== 和 equals() 29 實踐10:不要倚賴equals()的缺省實現33 實踐11:實現equals()時必須深思熟慮 43 實踐12:...
**Answer**: In Java, `String` objects are immutable, meaning their values cannot be changed once they are created. On the other hand, `StringBuffer` objects are mutable, allowing you to modify their ...
- **创建不可变对象**:另一种方法是设计不可变对象(Immutable Objects)。这意味着一旦对象被创建,它的状态就不能被更改。这通常通过使对象的所有字段成为final并且不允许任何外部修改操作来实现。这种方式可以...
14. **条目14:优先考虑使用不可变对象( Prefer Immutable Objects)** 不可变对象简化并发编程,减少错误,并且可以作为线程安全的缓存项。 15. **条目15:最小化可变对象的范围(Minimize Mutability)** 尽...
5. **不可变对象(Immutable Objects)** - 不可变对象是指一旦创建后其状态就不能被改变的对象。 - Java中的基本数据类型(如`int`、`long`)和String都是不可变的。 6. **Java内存模型** - Java内存模型规定了...