Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8404443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:36:07+00:00 2026-06-09T22:36:07+00:00

I have an immutable object that doesn’t have its fields or class marked as

  • 0

I have an immutable object that doesn’t have its fields or class marked as final. I could do this, but is there really any benefit to doing this? I could see it saving the compiler a little bit of time figuring things out, but I can’t see it being “worth” it (other then the fact that it will make future developers rethink doing something to the object to make it mutable).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:36:08+00:00Added an answer on June 9, 2026 at 10:36 pm

    Apart from the points you make (future developer modifying the fields being a very sensible one, another one is that someone could subclass your class and make it mutable), explicitly marking the fields as final provides you with visibility guarantees in a multi threaded environment.

    Take this class – it is effectively immutable:

    public class SomeClass {
        private int i;
        public SomeClass(int i) { this.i = i; }
        public int getI() { return this.i; }
    }
    

    In a multi-threaded environment, it is possible that a thread T1 creates a SomeClass sc = new SomeClass(1); and that another thread T2 reads sc.getI() and sees 0.

    If i is made final, this can not happen any more (assuming you don’t let this escape during construction, as explained in the quote below).

    Reference: JLS #17.5 – emphasis mine

    final fields also allow programmers to implement thread-safe immutable objects without synchronization. […]
    The usage model for final fields is a simple one: Set the final fields for an object in that object’s constructor; and do not write a reference to the object being constructed in a place where another thread can see it before the object’s constructor is finished. If this is followed, then when the object is seen by another thread, that thread will always see the correctly constructed version of that object’s final fields.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have a Java object that has only primitive fields marked as
I have some classes that represent immutable objects (Quantity, Price, Probability). Is there some
I want to have immutable Java objects like this (strongly simplified): class Immutable {
Say that I have an immutable Point class with x and y parameters, and
Lets say have this immutable record type: public class Record { public Record(int x,
I have come across a class that has an immutable property: MyObject[] allObjs The
Let's say I have a class Foo in Java that has immutable data: class
I have found that People claim that using all readonly fields in a class
I currently have an immutable type called Gene , that only has 2 fields:
This seems to be a fairly common problem, but the solutions that I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.