Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Final class and immutability

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Will all the classes declared with the keyword 'final' go immutable? (Just like String class and wrapper classes.)


Larsen.
 
Marshal
Posts: 25817
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Declaring a class "final" just says it can't be overridden. That doesn't affect its mutability in any way.
 
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want a class to be immutable you have to make all it's instance variables private and you shouldn't provide any setter methods that manipulate those instance variables. The classes can be made immutable by implementing it in such a way . Not by adding specifiers.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to make a class immutable, then one of the things you should do is make the class final.

But, as others have already said, making a class final does not make the class immutable (it's a necessary, but not a sufficient condition).
 
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Apart from declaring the class FINAl in order to make class immutable one must take care of two more things(as far as i know)

1). make fields of class PRIVATE and FINAL.
2). there must not any kind of setter method which may change or modify the state of object.

but i think something more is there to make class immutable.

Let say i want to create a immutable class , Immune , from above points.



i know its not immutable how can i create one ?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

naveen yadav wrote:i know its not immutable


What makes you think it's not immutable ?
 
Villains always have antidotes. They're funny that way. Here's an antidote disguised as a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic