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

  • Home
  • SEARCH
  • 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 6993945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:49:43+00:00 2026-05-27T19:49:43+00:00

Does this below class is immutable? class Immutable { private int x; Immutable(int value)

  • 0

Does this below class is immutable?

class Immutable {

  private int x;

  Immutable(int value) {
    this.x = value;
  }

  public int getX(){
    return x;
  }
}
  1. Class is not final Since no member is visible to sub class

  2. Instance Variable x is not final since there is no setter method.

Is there any possiblity this class will break the contract of Immutable functionality without adding any code in this class?

  • 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-27T19:49:44+00:00Added an answer on May 27, 2026 at 7:49 pm

    Joshua Bloch in his “Effective Java” states that “you should either design your classes for extensibility or make them non-extensible”.

    If flexibility is your primary concern and you have a strong reason to design your immutable class for inheritance – leave this class non-final.

    If security is your concern – make this class final and member field final. Because:

    • Non-final classes can be extended to create a mutable child classes by overriding getter method.

    • Java is reflective by design. As Belgther told it’s possible to set the value of member field using reflection. (no matter private or public) A long time ago I used this to integrate my specific debugger with another project.

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

Sidebar

Related Questions

I find myself lacking knowledge around the below. Take this class: public class MyClass
why does this code not work for referencing a const from a class? Background:
I have the class below. _commandLine variable is declared as static. Does this variable
Does anybody know how to do this syntax below in mysql? Without Stored Procedure,
In the below code sample, what does {0:X2} mean? This is from the reflection
Does this pattern: setTimeout(function(){ // do stuff }, 0); Actually return control to the
Does this code cause a memory leak: int main(){ int * a = new
Why does this javascript return 108 instead of 2008? it gets the day and
Why does this lambda expression not compile? Action a = () => throw new
I have this method: public static Expression<Func<MyEntity, bool>> MyMethod(string someId) { return o =>

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.