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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:16:16+00:00 2026-06-16T13:16:16+00:00

Could you please clarify that why final keyword is required before class when we

  • 0

Could you please clarify that why final keyword is required before class when we are making it an immutable one.
I mean, if we declare all of it’s attributes as private and final, then also it is an immutable class, isn’t it?

Sorry if the question seems easy, but i am truly confused about it. Help me out.

Editted:
I know that a class declared final can’t be subclassed.. But if each attribute is private and final then what difference does that make?

  • 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-16T13:16:17+00:00Added an answer on June 16, 2026 at 1:16 pm

    As stacker says, final makes sure the class isn’t subclassed. That’s important so that any code which is relying on its immutability can do so safely.

    For example, immutable types (where each field is also of an immutable type) can be freely used between threads without worrying about data races etc. Now consider:

    public class Person {
        private final String name;
    
        public Person(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    }
    

    That looks like you can share Person instances freely across threads with no problem. But what about when the object you’re sharing is actually a mutable subclass:

    public class Employee extends Person {
        private String company;
    
        public Employee(String name, String company) {
            super(name);
            this.company = company;
        }
    
        public void setCompany(String company) {
            this.company = company;
        }
    
        public String getCompany() {
            return company; 
        }
    }
    

    Now instances of Employee aren’t safe to share between threads, because they’re not immutable. But the code doing the sharing may only know about them as instances of Person… leading them into a false sense of security.

    The same goes for caching – it should be safe to cache and reuse immutable types, right? Well, it is safe to cache instances which are genuinely of an immutable type – but if you’re dealing with a type which itself doesn’t allow mutation, but does allow subclasses, it’s suddenly not safe any more.

    Think about java.lang.Object. It doesn’t have any mutable fields, but it’s clearly a bad idea to treat every Object reference as if it’s a reference to an immutable type. Basically it depends on whether you think about immutability as a property of the type or of objects. A truly immutable type declares “any time you see a reference of this type, you can treat it as immutable” – whereas a type which allows arbitrary subclassing can’t make that claim.

    As an aside, there’s a half-way house: if you can limit the subclassing to only “trusted” places, you can ensure that everything’s immutable, but still allow that subclassing. The access in Java makes that tricky, but in C# for example you could have a public class which only allowed subclassing within the same assembly – giving a public API which is nice and strong in terms of immutability, while still allowing for the benefits of polymorphism.

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

Sidebar

Related Questions

could you please clarify my issue? I have a starting page ListBox1.aspx that calls
I have a class with a number of attributes. Could someone please clarify the
Could you please clarify this part of Apple's documentation: Transitioning to ARC Release Notes
could anyone please clarify the meaning of line generalizes the tag object's storage of
Could you please clarify for me the question asked here . Why it is
Does APNS Feedback Service accounts all the reasons that could cause notifications delivery failure.
Could any Haskell experts out there please clarify something for me: Given a simplified
I have basic understanding problem with open source and licenses. Could someone please clarify
Could anyone please clarify for me the effect of Nginx caching image? Currently I
Wondering if someone could please explain the difference between these two queries and advise

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.