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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:58:07+00:00 2026-05-23T12:58:07+00:00

I am reading the book Effective Java. In an item Minimize Mutability , Joshua

  • 0

I am reading the book Effective Java.

In an item Minimize Mutability , Joshua Bloch talks about making a class immutable.

  1. Don’t provide any methods that modify the object’s state — this is fine.

  2. Ensure that the class can’t be extended. – Do we really need to do this?

  3. Make all fields final – Do we really need to do this?

For example let’s assume I have an immutable class,

class A{
private int a;

public A(int a){
    this.a =a ;
}

public int getA(){
    return a;
}
}

How can a class which extends from A , compromise A’s immutability ?

  • 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-05-23T12:58:07+00:00Added an answer on May 23, 2026 at 12:58 pm

    Like this:

    public class B extends A {
        private int b;
    
        public B() {
            super(0);
        }
    
        @Override
        public int getA() {
            return b++;
        }
    }
    

    Technically, you’re not modifying the fields inherited from A, but in an immutable object, repeated invocations of the same getter are of course expected to produce the same number, which is not the case here.

    Of course, if you stick to rule #1, you’re not allowed to create this override. However, you cannot be certain that other people will obey that rule. If one of your methods takes an A as a parameter and calls getA() on it, someone else may create the class B as above and pass an instance of it to your method; then, your method will, without knowing it, modify the object.

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

Sidebar

Related Questions

I'm reading the Effective Java book by Joshua Bloch. In the first chapter, he
I'm reading Effective Java Second Edition by Joshua Bloch and im confused by below
I am reading Josh Bloch's book Effective Java and he suggests using a builder
So I've been reading Effective Java by Joshua Bloch and noticed two points which
I'm reading the book of Meyers about effective c++ programming, in item 25 I
I am reading Bill Wagner's book Effective C# . In Item 32 he is
I am reading Effective Java and the book has the below comment on the
I am reading a book about Java and it says that you can declare
After reading a book on LINQ I'm thinking about re-writing a mapper class that
recently I've been reading through Scott Meyers's excellent Effective C++ book. In one of

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.