什么是子宫憩室| calcium是什么意思| 庚午日五行属什么| lhrh是什么激素| 做梦笑醒了有什么征兆| 猪八戒是什么佛| 冰妹什么意思| 忠诚是什么意思| 两个立念什么| 为什么会牙龈出血| 什么是ps| 牙痛挂什么科| 牛肉不能跟什么一起吃| 副词是什么| 手掌发热是什么原因| 孕妇口腔溃疡能用什么药| 智是什么意思| pdd是什么| 咖啡有什么作用| 六味地黄丸起什么作用| 急性肠胃炎是什么引起的| 验孕棒什么时候测| 腹泻不能吃什么食物| 什么的散步| 尿酸偏高是什么病| 西替利嗪是什么药| 吃螃蟹不能吃什么| 麻木是什么原因引起的| 嗓子发炎挂什么科| 把子肉是什么| 咖色裤子配什么颜色上衣| 武松打的是什么虎| 桃胶什么时候采摘最好| 我想长胖点有什么办法| 蟑螂为什么会飞| 物理意义是什么意思| 护理专业是干什么的| 味粉是什么调料| 马中赤兔人中吕布什么意思| 负离子是什么| 梦到丧尸是什么预兆| 低血糖是什么| 输卵管堵塞有什么症状| 59年属什么生肖| 狗狗咬主人意味着什么| 马齿苋别名叫什么| 断档是什么意思| 睡觉起来嘴巴苦是什么原因| lh是什么| 国士无双什么意思| 什么药治高血压效果最好| 提供什么| 5.20什么星座| 白色糠疹用什么药膏| 窥视是什么意思| 睾酮低有什么影响| 辐射对人体有什么伤害| 尿酸520属于什么水平| 大学硕士点是什么意思| 饕餮什么意思| 糖尿病吃什么主食| 插入阴道什么感觉| 抗体和抗原有什么区别| 非转基因是什么意思| 全日制专科是什么意思| 干红是什么意思| 后脑勺出汗多是什么原因| 十周年是什么婚| 12岁属什么| 龟头发炎用什么药| 动卧是什么意思| 头疼恶心是什么症状| 牛叉是什么意思| 幽门杆菌的症状是什么| 草莓印是什么| 月经前是什么期| 临床什么意思| 面部提升做什么项目最好| 天秤男喜欢什么样的女生| 性瘾是什么意思| 马首是瞻是什么生肖| 虾不能和什么水果一起吃| 肾脏炎有什么症状| 鸡尾酒是什么意思| 六月十八是什么星座| 银屑病是什么| 脂肪瘤应该挂什么科| 为什么不建议年轻人做肠镜| 脚痛挂什么科| 眼角长痘痘是什么原因| 生蚝是什么东西| 毛囊炎挂什么科| 三轮体空是什么意思| 什么样的升旗仪式| 涅盘是什么意思| 灵芝长在什么地方| 扁桃体结石吃什么药| 颈椎骨质增生吃什么药效果好| 温水煮青蛙什么意思| 海之蓝是什么香型| 又字加一笔是什么字| 心什么什么什么| 排卵期后面是什么期| 睾丸炎用什么药| 天德是什么生肖| cut什么意思| 枫字五行属什么| 坐骨神经痛吃什么药好得快| 小蝌蚪吃什么| 秦始皇原名叫什么| 补休是什么意思| 小资情调是什么意思| 霉菌性阴道炎用什么药最好| 海石花是什么| 香蕉对身体有什么好处| 小猫打什么疫苗| fsh是什么意思| 炸酱面用的什么酱| 闭口是什么样子| 什么人容易得白血病| 鳞状上皮内低度病变是什么意思| 狗眼看人低是什么意思| 气郁症是什么症状| 肚子胀气吃什么药好| 吃榴莲不能和什么一起吃| 头发的主要成分是什么| 什么是生理期| 查甲亢挂什么科| 什么药是消炎药| 不过是什么意思| tg什么意思| 皮肤消炎用什么药最好| 拔了尿管尿不出来有什么好办法| 一级军士长什么待遇| 尿微量白蛋白高是什么原因| 七月11日是什么星座| 情人眼里出西施是什么心理效应| 小狗肚子里有虫子吃什么药| 交链孢霉过敏是什么| 什么是包茎| 强直性脊柱炎看什么科| 西瓜有什么功效| 丑未戌三刑 会发生什么| 陈赫开的火锅店叫什么| 狗狗为什么会咬人| g点是什么| 骨折不能吃什么东西| 吃什么排铜最快| 秀女是什么意思| 去湿气吃什么药| 检查胸部应该挂什么科| 梦到水是什么意思| 鹿沼土是什么土| 计算机二级什么时候考| 肺静脉流的是什么血| conch是什么牌子| balmain什么档次| 什么是义眼| 摘环后需要注意什么| 神的国和神的义指的是什么| 子宫内膜异位症吃什么药| 综合能力测试考什么| 长沙开福寺求什么最灵| 干眼症有什么症状| 营卫是什么意思| 喘不上来气是什么原因| 双侧卵巢显示不清是什么意思| 核桃和什么一起打豆浆| 精彩是什么意思| 男性查hpv挂什么科| 言谈举止是什么意思| 腰间盘突出压迫神经腿疼吃什么药| 大便干燥一粒一粒的是什么原因| 贫血吃什么东西好| 腐男是什么意思| anti什么意思| 嗓子疼吃什么药| 纳豆什么味道| 子宫后位什么意思| 什么的时间| 百年灵手表什么档次| 肾虚挂什么科| 骨骼肌率是什么意思| 胃动力不足是什么原因造成的| 大便干燥一粒一粒的是什么原因| screenx影厅是什么| qty什么意思| 什么花草| 什么地唱| 过期的酸奶有什么用途| 真狗是什么意思| 凤尾菜又叫什么菜| KTV服务员主要做什么| 生精补精吃什么药最快| 梦见被雨淋是什么意思| 本帮菜是什么意思| 被蝎子蛰了用什么药| 呼吸不顺畅是什么原因| 九月三号是什么星座| 血沉偏高说明什么| 1977年出生属什么生肖| 蜈蚣吃什么食物| 辟支佛是什么意思| 痕迹是什么意思| 婴幼儿屁股红擦什么| 鹦鹉鱼吃什么| 去皱纹用什么方法最好和最快| 乳酸偏高是什么意思| 激素六项检查挂什么科| sr是什么意思| 六月十三日是什么日子| 怀孕期间吃什么对胎儿发育好| 阴道炎用什么洗液| 毛囊炎吃什么药| 人生苦短是什么意思| 两横两竖是什么字| 夫妻分房睡意味着什么| 树根有什么作用| 什么无云| 查肾功能挂什么科| 双子座是什么性格| 为什么喝酒后会头疼| 腿抽筋是什么原因造成的| 通马桶的工具叫什么| 布谷鸟什么时候叫| 血小板是什么颜色的| 体检挂什么科| 对宫星座是什么意思| 丙肝有什么症状表现| 床盖是什么| 褶是什么意思| 心肌梗塞是什么原因造成的| 九重紫纪咏结局是什么| 左脸颊有痣代表什么| 梦见老公有外遇预示什么| 为什么尽量不打免疫球蛋白| 什么什么不见| 什么食物含维生素c最多| 排酸肉是什么意思| 胆结石有什么治疗方法| 梅花表属于什么档次| 杜字五行属什么| 前列腺炎不治疗有什么后果| 什么玉最值钱| 吐黄痰是什么原因| 校正是什么意思| 有什么好| 外科医生是做什么的| trust什么意思| 非钙化斑块是什么意思| 膝关节积液吃什么药| 一什么饼干| ab是什么血型| beko是什么牌子| baleno是什么牌子| 日本兵为什么不怕死| 桃花开在什么季节| q币有什么用| 学字五行属什么| 温州人为什么会做生意| 得了艾滋病会有什么症状| 腿部青筋明显是什么原因| 惊弓之鸟什么意思| 妇科衣原体感染是什么病| 百度
DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Attention Kubernetes users! DZone is researching how K8s usage is evolving in 2025 — tell us the good, the bad, + the frustrating.

Getting Started With DevSecOps: This Refcard provides actionable insights for embedding security into your DevOps culture and workflows.

Data Engineering: Not your average "AI is changing everything" report. Dive into tooling, strategy, + systems in scalable data ecosystems.

Java Caching: This Refcard explores core caching concepts and demonstrates how to implement them using Java’s JCache API.

Related

  • Mastering Thread-Local Variables in Java: Explanation and Issues
  • Unlocking Performance: Exploring Java 21 Virtual Threads [Video]
  • Visualizing Thread-Safe Singletons in Java
  • Double-Checked Locking Design Pattern in Java

Trending

  • ArchUnit, Unit Testing The Architecture
  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  • The Golden Signals of Monitoring
  • Vibe Coding: Conversational Software Development — Part 3: Prompt Discipline
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. The Importance of Immutability in Java

民事诉讼中共同被告其中一个是担保人另一...

We all know immutability is important, but do you know why or how to achieve it in Java? This post is a one stop shop guide to the challenge.

By 
Sam Atkinson user avatar
Sam Atkinson
·
Apr. 05, 16 · Opinion
Likes (27)
Comment
Save
Tweet
Share
36.0K Views
百度 ”王宏伟说。

Join the DZone community and get the full member experience.

Join For Free

One of the consistent criticisms of Java is that it lacks a formal immutable type. We can (and should) make a good attempt at creating immutable Objects, even though they may be inherently flawed due to the nature of the JVM.

An immutable object is one whose state cannot and will not change after it’s initial creation. Immutable objects are great, mostly because they are Thread safe (and threaded code should be avoided as much as possible). You can pass them around without fear they will be changed. I highly recommend you spend some time with a functional language like Scala to really appreciate the amazing power immutable Objects can have (but then come straight back, because Scala has a whole different bag of problems).

Interestingly, almost all of the new features in Java 8 (Date and Time, Optionals and Streams) have been implemented in an immutable fashion. This allows much of the performance benefits that can come from things such as parallel Streams. Immutable Objects allow us to create side-effect free functions as seen in Functional Programming languages which are the basis for creating fast, lock free code.

How to Create an Immutable Object in Java?

The main action is to mark all fields as final. This obviously means they cannot change after initial construction. Beware though, that you can still have a final field where the Object contained is mutable. In this case it is necessary to copy the Object when initially set in the constructor, and provide a copy of the Object when it is being accessed from outside the class.

This obviously adds complication to our code and design. Ideally you should follow the original advice from Effective Java:

"Classes should be immutable unless there's a very good reason to make them mutable....If a class cannot be made immutable, limit its mutability as much as possible."

If you have an Object field in your class endeavour where possible to make it immutable too.

If you succeed in making all your fields immutable then you may choose to also make them public as I do- the fields cannot be changed post construction and are only used for reading. This makes the addition of a getter redundant. In my code bases if I’m accessing data using the fields then I know that class is Immutable. The main exception to this is when I’m writing libraries, as it’s much harder to refactor if you need to introduce mutability later on. If you own all the code though, to move from field access to method access is one shortcut in IntelliJ.

You should also make your class final to defend from subclassing. It would be possible to create mutable subclasses and thus ruining your hard work.

It takes a concious effort to create immutable classes but it should be your target wherever possible. A common anti-pattern I’ve seen in developers is that after creating a constructor and set of fields they generate getters/setters for all of them. Do not do this! Firstly, code should only be written if it is needed; if you have no test or prod code that is accessing a field then it does not need a getter; if no other code is trying to change the field after creation then you don’t need a setter. Create code on demand and do not optomize early.

One of the main criticisms of immutable objects is that it can lead to a proliferation of objects and as a result performance issues- have the potential for a significant amount of churn of new objects as you’re having to create a new one for any state change.  Unless you're in a crazy high performance environment (and you're almost certainly not) then it really isn't an issue.  Objects are cheap. Even Oracle thinks so:

“The impact of object creation is often overestimated and can be offset by some of the efficiency associated with immutable objects. These include decreased overhead due to garbage collection, and the elimination of code needed to protect mutable objects from corruption.”

This is a classic case of eagerly optomising code. If you create immutable code and it turns out you’re hitting massive performance roadblocks then refactor. For most cases you’ll be fine. There are obvious exceptions; data structures tend to be much easier to implement and more performant if using mutability. As always, apply common sense to your approach, but default to immutability.

Java (programming language) Threading

Opinions expressed by DZone contributors are their own.

Related

  • Mastering Thread-Local Variables in Java: Explanation and Issues
  • Unlocking Performance: Exploring Java 21 Virtual Threads [Video]
  • Visualizing Thread-Safe Singletons in Java
  • Double-Checked Locking Design Pattern in Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

胃不好的人适合吃什么水果 鸡枞菌长在什么地方 2010年什么年 痛风急性发作期吃什么药 前胸后背长痘痘用什么药
梦见红色的蛇是什么意思 乳腺检查挂什么科 镜检白细胞是什么意思 马不停蹄是什么生肖 大家闺秀是什么生肖
桃花的花语是什么 七夕送老婆什么 硬盘是什么 叹气是什么意思 晓五行属什么
1921年属什么生肖 洋盘是什么意思 6.26什么星座 窝窝头是用什么做的 疫情是什么
努尔哈赤是什么民族hcv9jop4ns9r.cn 硬气是什么意思hcv8jop6ns3r.cn 护照需要什么材料hcv7jop9ns3r.cn 左下腹痛是什么原因zsyouku.com 什么季节最短hcv8jop8ns5r.cn
6.19什么星座hcv8jop3ns3r.cn 胃疼吃什么水果hcv7jop7ns3r.cn 胃溃疡吃什么药hcv8jop2ns1r.cn 做梦吃饺子是什么意思hebeidezhi.com 宝宝肠胃炎吃什么药jasonfriends.com
为什么不能摸猫的肚子hcv8jop8ns9r.cn 莲蓬什么季节成熟hcv8jop0ns0r.cn 直肠炎是什么原因引起的hcv7jop5ns6r.cn 腊肉炒什么菜最好吃hcv8jop2ns9r.cn 背痛去医院挂什么科hcv7jop9ns3r.cn
除皱针什么牌子效果最好hcv8jop7ns8r.cn 小孩贫血有什么症状hcv8jop8ns7r.cn 梦见蛇咬别人是什么意思xinmaowt.com 乐的五行属性是什么hcv8jop5ns7r.cn 牙齿出血是什么病hcv8jop6ns0r.cn
百度