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 8486299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:54:17+00:00 2026-06-10T20:54:17+00:00

I read that to make a class immutable in Java, we should do the

  • 0

I read that to make a class immutable in Java, we should do the following,

  1. Do not provide any setters
  2. Mark all fields as private
  3. Make the class final

Why is step 3 required? Why should I mark the class final?

  • 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-10T20:54:19+00:00Added an answer on June 10, 2026 at 8:54 pm

    If you don’t mark the class final, it might be possible for me to suddenly make your seemingly immutable class actually mutable. For example, consider this code:

    public class Immutable {
         private final int value;
    
         public Immutable(int value) {
             this.value = value;
         }
    
         public int getValue() {
             return value;
         }
    }
    

    Now, suppose I do the following:

    public class Mutable extends Immutable {
         private int realValue;
    
         public Mutable(int value) {
             super(value);
           
             realValue = value;
         }
    
         public int getValue() {
             return realValue;
         }
         public void setValue(int newValue) {
             realValue = newValue;
         }
    
        public static void main(String[] arg){
            Mutable obj = new Mutable(4);
            Immutable immObj = (Immutable)obj;              
            System.out.println(immObj.getValue());
            obj.setValue(8);
            System.out.println(immObj.getValue());
        }
    }
    

    Notice that in my Mutable subclass, I’ve overridden the behavior of getValue to read a new, mutable field declared in my subclass. As a result, your class, which initially looks immutable, really isn’t immutable. I can pass this Mutable object wherever an Immutable object is expected, which could do Very Bad Things to code assuming the object is truly immutable. Marking the base class final prevents this from happening.

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

Sidebar

Related Questions

In Java, how can you make an ArrayList read-only (so that no one can
I created an ssh2 wrapper. I have read that a constructor should not fail,
Just a simple question: I have read that a class should be made static
i'm developing an app that make requests to the Musicbrainz webservice. I read in
I know that read() is a blocking call unless I make the socket non-blocking.
How can I make an interactive function that interactively read a key from the
I want to make a program that will read some number in string format
Is there a way to make the UIPickerView read-only? This means that the user
I read this excellent article and it make sense: Java is strictly pass by
I feel that this is something I should know, but does a java agent

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.