• Post Reply Bookmark Topic Watch Topic
  • New Topic

immutable  RSS feed

 
cherry hal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to create immutable class without using final keyword?
please give me a sample code for this?
 
Jesus Angeles
Ranch Hand
Posts: 2071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is immutable.



If you make a virus with type = 1, it can never mutate to any other BirdFluVirus type, e.g. type = 2.

Below is a mutable one.




[Edited: Added 'private' to correct code, as per Matthew's post.]
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello cherry hal,

Welcome to CodeRanch!

cherry hal wrote:How to create immutable class without using final keyword?

Why do you want to do that? I mean to ask - is it part of some assignment, or some project requirement (so that we can see if this can be avoided etc.)
cherry hal wrote:please give me a sample code for this?

Well, this is NotACodeMill. There are ways to create immutable class, but we would appreciate to see your code and hear about what problems you are facing about it.

I hope this helps.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesus Angeles wrote:This is immutable.


Actually, it isn't, because it's got one important flaw. The type variable needs to be private, otherwise anyone can change the value. Other than that, though, you're right. The key part of making a class immutable is simply to not provide any way of changing the state.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have always found this as an excellent article on the topic of java immutability
http://www.javaranch.com/journal/2003/04/immutable.htm
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cherry hal wrote:How to create immutable class without using final keyword?

With care. In addition to what Matthew said about Anayonkar's solution, there's also the fact that it can be subclassed.
This means that anyone can create a mutable variant which, even if it can't actually change any of the superclass's state, could still render any claims of Thread-safety invalid.

Another possibility is:The advantage of this pattern is that you can restrict subtyping.

For example, if the constructor was package-private, then the class could only be subclassed by another class in the same package, which is probably a lot safer than letting anyone do it.

Winston (with kudos to Maneesh)
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another (perhaps simpler) way to make it impossible to subclass a class is by making the class itself final:

Then you wouldn't have to make the constructor private and the and the getter method final (they can't be overridden since the class can't be subclassed).

Note that in Winston's example it also wasn't really necessary to make the getter method final, because the constructor is private, which means you can't subclass it and override the method.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Note that in Winston's example it also wasn't really necessary to make the getter method final, because the constructor is private, which means you can't subclass it and override the method.

Ah, maybe you didn't read my postscript; and for that it must be.
Also, cheery's question was specifically about whether it's possible to make a class immutable without using final.

Now, whether you''d want to is another matter...

Winston
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!